Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] Cannot remove value in repeatable #5778

Open
cod3rshotout opened this issue Feb 20, 2025 · 5 comments
Open

[Bug] Cannot remove value in repeatable #5778

cod3rshotout opened this issue Feb 20, 2025 · 5 comments
Labels

Comments

@cod3rshotout
Copy link

I'm using the latest backpack version with pro addon, this is my field:

CRUD::addField([
  'name' => 'users',
  'type' => 'relationship',
  'label' => __('backend/dealership.managers'),
  'model' => 'App\Models\User',
  'attribute' => 'fullName',
  'pivot' => false,
  'inline_create' => ['entity' => 'user'],
  'data_source' => route('dealership.fetchEdpUsers'),
  'minimum_input_length' => 0
]);

essentially a dealership can have many users, but a user can only have one dealership.
This is the relationship on Dealership model:

 public function users() {
    return $this->hasMany(User::class);
  }

and this is the relationship on User model:

   public function dealership()
   {   
      return $this->belongsTo(Dealership::class);
   }

When I save the users on the Dealership controller everything works fine, but when I remove one user from the repeatable and then save I get:

SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: 'NULL' for column razor.users.dealership_id at row 1 (Connection: mysql, SQL: update users set dealership_id = NULL, users.updated_at = 2025-02-20 12:42:47 where id not in (5) and dealership_id = 1)

The column dealership_id is set to nullable on the users table, I don't understand

Field Type Null Key Default Extra  
id bigint(20) unsigned NO PRI NULL auto_increment
first_name varchar(255) YES   NULL  
last_name varchar(255) YES   NULL  
business_name varchar(255) YES   NULL  
email varchar(255) NO UNI NULL  
dealership_id bigint(20) unsigned YES MUL NULL  
email_verified_at timestamp YES   NULL  
password varchar(255) NO   NULL  
remember_token varchar(100) YES   NULL  
created_at timestamp YES   NULL  
updated_at timestamp YES   NULL  
Copy link

Based on the information provided in the knowledge sources and your question, it appears you're encountering a bug related to removing values in a repeatable field using Backpack with the Pro addon. This issue is similar to problems reported in several GitHub issues for Backpack.

The error you're experiencing seems to be related to how Backpack handles the deletion of related entries in a HasMany relationship. When you remove a user from the repeatable field and save, Backpack is trying to update the dealership_id to NULL instead of deleting the relationship entirely.

There are a couple of potential solutions you can try:

  1. Add 'force_delete' => true to your field definition:
CRUD::addField([
    'name' => 'users',
    'type' => 'relationship',
    // ... other attributes ...
    'force_delete' => true,
]);

This option was mentioned as a workaround in Github Issue #4204.

  1. If the above doesn't work, you might need to wait for or apply a fix that was developed for a similar issue. As mentioned in Github Issue #4205, there was a pull request to address this kind of problem. You could try updating to the latest version of Backpack, as this fix might have been incorporated.

  2. If neither of the above solutions work, you might need to override the update() method in your CrudController to handle the deletion of related entries manually.

It's worth noting that the error message you're receiving suggests that the dealership_id column might not actually be nullable in the database, despite what the schema shows. You might want to double-check your database structure and migrations to ensure that the column is indeed nullable.

If these solutions don't resolve your issue, it would be advisable to report this as a new bug on the Backpack GitHub repository, as it might be a specific edge case not covered by existing fixes.

ℹ️ Please note our AI Bot does not reply to follow-ups, this will be the only AI answer in this thread. If the AI helped you find the answer, please mark it as answered or write details below so it can learn. Then close the conversation. If not, please give more information for when a human reads this. ℹ️

Relevant sources:

Fields|Using the table in a repeatable field
Github Issue (closed): [Bug] disabling ConvertEmptyStringsToNull has side effects on the saving process for M-M relations
Github Issue (closed): [Bug] Save relationship data using DB transaction
Github Issue (closed): [Bug] Cannot update data model after 4.1.44 update
Github Issue (closed): [4.1] Relationship field does not work inside repeatable
Github Issue (closed): [Bug] Original data persists when emptying a relationship field with inline_create
Github Issue (closed): [Bug] Repeatable field with belongsToMany relationship not saving data
Github Issue (closed): [4.1][Bug] relationship column doesn't work -
Github Issue (closed): [Bug] Error when trying to save a subfield in a relationship field
Github Issue (closed): [Bug] Unable to save empty input with relation HasOne
Github Issue (closed): [Bug] Deleting last relation tries to update this record and throws an error
Github Issue (closed): [Bug] nullable date in repeatable field is not working
Github Issue (closed): [Bug] hasMany fields should delete repeatable entries by default

@cod3rshotout
Copy link
Author

Also, I noticed right now that the create inline operation button doesn't appear on my dealership crud controller, but on the user controller appear correctly, in the user controller I simply have defined as the following:

$this->crud->addField([
  'name' => 'dealership_id',
  'type' => 'relationship',
  'entity' => 'dealership',
  'label' => __('backend/user.dealership'),
  'ajax' => true,
  'tab' => $general_tab,
  'data_source' => route("user.fetchDealerships"),
  'attribute' => 'business_name',
  'wrapper' => [
    'class' => 'form-group col-md-6'
  ],
  'inline_create' => [
    'entity' => 'dealership',
    'force_select' => true,
    'create_route' => route('dealership-inline-create-save'),
    'add_button_label' => __('backend/user.add_new_dealership'),
  ],
  'minimum_input_length' => 0
]);

could I please get an help?

@jcastroa87
Copy link
Member

Hello @cod3rshotout

Did you try to add to the relationship

'force_delete' => true,

This work for you?

Cheers.

@cod3rshotout
Copy link
Author

Hi @jcastroa87 yes I tried that option but if I set force_delete true, then the entry will be removed, I don't need this behavior. I need a repeater on the dealership controller that allows me to assign to that dealer a list of users. The association between user and dealers is done using the dealership_id column available on the user table. I don't have a pivot table since a user can only have one dealership associated. Also, the inline create button doesn't appear

@cod3rshotout
Copy link
Author

@pxpm @tabacitu @jcastroa87 please could I get a feedback about this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: No status
Development

No branches or pull requests

2 participants