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] Delete the last item in relationship (with subfileds) with DB col constrained #5742

Closed
kiddtang opened this issue Dec 31, 2024 · 1 comment
Labels

Comments

@kiddtang
Copy link
Contributor

Bug report

A simple relationship field with subfield

[
    'name' => 'medias',
    'label' => 'Product Images & Videos',
    'type' => 'relationship',
    'subfields' => [
        [
            'name' => 'enabled',
            'label' => 'Enabled',
            'type' => 'switch',
            'color' => '#42ba96',
            'default' => true,
        ],
    ],
    'new_item_label' => 'Add Product Media',
    'init_rows' => 0,
    'min_rows' => 0,
    'reorder' => 'sort_order',
],

[MUST] with SQL Constraints

$table->foreignId('product_id')->constrained('products')->cascadeOnDelete();

What I did

  1. Create one item
    image
  2. Delete the Last item

What I expected to happen

the LAST item removed without SQL error, just like delete the other items.

What happened

it tried to set product_id = 0 which trigger Integrity constraint violation.

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`laravel`.`product_medias`, CONSTRAINT `product_medias_product_id_foreign` FOREIGN KEY (`product_id`) REFERENCES `products` (`id`) ON DELETE CASCADE) (Connection: mysql, SQL: update `product_medias` set `product_id` = 0, `product_medias`.`updated_at` = 2024-12-31 23:50:55 where `product_medias`.`product_id` = 1002 and `product_medias`.`product_id` is not null)

What I've already tried to fix it

remove the SQL Constraints

$table->foreignId('product_id');

This works perfectly.

Is it a bug in the latest version of Backpack?

After I run composer update backpack/crud the bug... is it still there?
Yes

Backpack, Laravel, PHP, DB version

When I run php artisan backpack:version the output is:

PHP VERSION:

8.3.12

PHP EXTENSIONS:

Core, date, libxml, openssl, pcre, zlib, filter, hash, json, pcntl, random, Reflection, SPL, session, standard, sodium, mysqlnd, PDO, xml, bcmath, calendar, ctype, curl, dom, mbstring, FFI, fileinfo, ftp, gd, gettext, iconv, igbinary, imagick, imap, intl, ldap, exif, msgpack, mysqli, pcov, pdo_mysql, pdo_pgsql, pdo_sqlite, pgsql, Phar, posix, readline, shmop, SimpleXML, soap, sockets, sqlite3, sysvmsg, sysvsem, sysvshm, tokenizer, xmlreader, xmlwriter, xsl, zip, memcached, redis, swoole, Zend OPcache, xdebug

LARAVEL VERSION:

11.36.1.0

BACKPACK PACKAGE VERSIONS:

backpack/basset: 1.3.6
backpack/crud: 6.7.45
backpack/filemanager: 3.0.10
backpack/generators: v4.0.7
backpack/medialibrary-uploaders: 1.2.1
backpack/permissionmanager: 7.2.1
backpack/pro: 2.2.29
backpack/settings: 3.1.1
backpack/theme-tabler: 1.2.17

Additional Information

Just a normal Product has Many ProduceMedia

class Product extends Model
{
    use CrudTrait;
    
    public function medias(): HasMany
    {
        return $this->hasMany(ProductMedia::class);
    }
}
Copy link

Based on the information provided in the knowledge sources, this issue appears to be a known bug in Backpack CRUD that has been reported and addressed in previous versions. The problem you're experiencing is related to how Backpack handles the deletion of the last item in a HasMany relationship field with subfields.

This issue is similar to the one described in Github Issue #4187, where deleting the last entry in a HasMany relationship tries to update the entry with a null foreign key instead of deleting it.

The solution to this problem was implemented in Pull Request #4205, which addressed the issue of deleting the last entry in HasMany relationships.

However, since you're using the latest version of Backpack (6.7.45) and still experiencing this issue, it seems that the problem may have resurfaced or wasn't completely resolved for all scenarios.

To work around this issue, you can try the following:

  1. Add the 'force_delete' => true attribute to your relationship field definition:
[
    'name' => 'medias',
    'label' => 'Product Images & Videos',
    'type' => 'relationship',
    'force_delete' => true,
    // ... other attributes
]

This should force Backpack to delete the related entries instead of trying to update them with a null foreign key.

  1. If the above doesn't work, you may need to override the update method in your CrudController to handle the deletion of related entries manually. However, this is not ideal and should be considered a temporary workaround.

I recommend reporting this issue to the Backpack GitHub repository, as it seems to be a regression of a previously fixed bug. Provide them with the details you've shared here, including your Backpack version and the specific field configuration you're using.

Github Issues for Backpack CRUD is the best place to report this bug and get assistance from the Backpack maintainers.

ℹ️ 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:

Github Issue (closed): [Bug] Deleting last relation tries to update this record and throws an error
Github Issue (closed): Relationship field does not remove deleted related items from database.
Github Issue (closed): [4.1][Bug] field and filter names being changed by Backpack
Github Issue (open): [Bug] Items with a HasMany relationship cannot be deleted in the relationship field
Fix relationship saving error in the curd operation
Github Issue (closed): [Bug] hasMany relationship field not working
Github Issue (closed): [Bug] FakeFields: Undefined array key "model"
Github Issue (closed): [Bug] hasMany fields should delete repeatable entries by default
Github Issue (closed): [Bug] Update Trait: bad array key checking $field['subfield']
Github Issue (closed): [4.1][Bug] relationship column doesn't work -
Github Issue (closed): [WIP][REFERENCE] Relationships know limitations / edge cases
Github Issue (closed): [Bug] when addField is used to add from a relationship it's very slow
Github Issue (closed): [Bug] Original data persists when emptying a relationship field with inline_create
Github Issue (closed): [Bug] Error when trying to save a subfield in a relationship field

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

No branches or pull requests

1 participant