filamentphp / filament

A collection of beautiful full-stack components for Laravel. The perfect starting point for your next app. Using Livewire, Alpine.js and Tailwind CSS.
https://filamentphp.com
MIT License
19.14k stars 2.95k forks source link

Foreign key constraint failure on import table when using a different auth provider than users #13119

Closed likeabas closed 5 months ago

likeabas commented 5 months ago

Package

filament/filament

Package Version

v3.2.79

Laravel Version

v11.7.0

Livewire Version

v3.4.12

PHP Version

8.2

Problem description

When you change the users provider table to another table than users, you get a foreign key constraint failure on the imports table, because the imports_user_id_foreign refers to the users table.

Expected behavior

No foreign key constraint to the user table in this case.

Steps to reproduce

  1. Setup filament and create an import action
  2. Create another model for the filament users (e.g. Admin model) with another table than users.
  3. Setup this model as provider in config/auth.php and in your admin panel using these steps in the docs.
  4. Create a row in your auth model with an id that's not in your users table
  5. Run an import

Reproduction repository

https://github.com

Relevant log output

[previous exception] [object] (PDOException(code: 23000): SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`villagracia`.`imports`, CONSTRAINT `imports_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE) at /home/forge/villagracia.nl/vendor/laravel/framework/src/Illuminate/Database/Connection.php:571)
[stacktrace]
#0 /home/forge/villagracia.nl/vendor/laravel/framework/src/Illuminate/Database/Connection.php(571): PDOStatement->execute()
#1 /home/forge/villagracia.nl/vendor/laravel/framework/src/Illuminate/Database/Connection.php(800): Illuminate\\Database\\Connection->Illuminate\\Database\\{closure}()
#2 /home/forge/villagracia.nl/vendor/laravel/framework/src/Illuminate/Database/Connection.php(767): Illuminate\\Database\\Connection->runQueryCallback()
#3 /home/forge/villagracia.nl/vendor/laravel/framework/src/Illuminate/Database/Connection.php(560): Illuminate\\Database\\Connection->run()
#4 /home/forge/villagracia.nl/vendor/laravel/framework/src/Illuminate/Database/Connection.php(524): Illuminate\\Database\\Connection->statement()
#5 /home/forge/villagracia.nl/vendor/laravel/framework/src/Illuminate/Database/Query/Processors/Processor.php(32): Illuminate\\Database\\Connection->insert()
#6 /home/forge/villagracia.nl/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php(3659): Illuminate\\Database\\Query\\Processors\\Processor->processInsertGetId()
#7 /home/forge/villagracia.nl/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(2026): Illuminate\\Database\\Query\\Builder->insertGetId()
#8 /home/forge/villagracia.nl/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1338): Illuminate\\Database\\Eloquent\\Builder->__call()
#9 /home/forge/villagracia.nl/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1303): Illuminate\\Database\\Eloquent\\Model->insertAndSetId()
#10 /home/forge/villagracia.nl/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1142): Illuminate\\Database\\Eloquent\\Model->performInsert()
#11 /home/forge/villagracia.nl/vendor/filament/actions/src/Concerns/CanImportRecords.php(214): Illuminate\\Database\\Eloquent\\Model->save()
#12 /home/forge/villagracia.nl/vendor/filament/support/src/Concerns/EvaluatesClosures.php(35): Filament\\Actions\\ImportAction->Filament\\Actions\\Concerns\\{closure}()
#13 /home/forge/villagracia.nl/vendor/filament/actions/src/MountableAction.php(41): Filament\\Support\\Components\\Component->evaluate()
#14 /home/forge/villagracia.nl/vendor/filament/actions/src/Concerns/InteractsWithActions.php(94): Filament\\Actions\\MountableAction->call()
#15 /home/forge/villagracia.nl/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(36): Filament\\Pages\\BasePage->callMountedAction()
#16 /home/forge/villagracia.nl/vendor/laravel/framework/src/Illuminate/Container/Util.php(41): Illuminate\\Container\\BoundMethod::Illuminate\\Container\\{closure}()
#17 /home/forge/villagracia.nl/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(93): Illuminate\\Container\\Util::unwrapIfClosure()
#18 /home/forge/villagracia.nl/vendor/laravel/framework/src/Illuminate/Container/BoundMethod.php(35): Illuminate\\Container\\BoundMethod::callBoundMethod()
#19 /home/forge/villagracia.nl/vendor/livewire/livewire/src/Wrapped.php(23): Illuminate\\Container\\BoundMethod::call()
#20 /home/forge/villagracia.nl/vendor/livewire/livewire/src/Mechanisms/HandleComponents/HandleComponents.php(474): Livewire\\Wrapped->__call()
#21 /home/forge/villagracia.nl/vendor/livewire/livewire/src/Mechanisms/HandleComponents/HandleComponents.php(101): Livewire\\Mechanisms\\HandleComponents\\HandleComponents->callMethods()
#22 /home/forge/villagracia.nl/vendor/livewire/livewire/src/LivewireManager.php(97): Livewire\\Mechanisms\\HandleComponents\\HandleComponents->update()
#23 /home/forge/villagracia.nl/vendor/livewire/livewire/src/Mechanisms/HandleRequests/HandleRequests.php(93): Livewire\\LivewireManager->update()
#24 /home/forge/villagracia.nl/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(46): Livewire\\Mechanisms\\HandleRequests\\HandleRequests->handleUpdate()
#25 /home/forge/villagracia.nl/vendor/laravel/framework/src/Illuminate/Routing/Route.php(260): Illuminate\\Routing\\ControllerDispatcher->dispatch()
#26 /home/forge/villagracia.nl/vendor/laravel/framework/src/Illuminate/Routing/Route.php(206): Illuminate\\Routing\\Route->runController()
#27 /home/forge/villagracia.nl/vendor/laravel/framework/src/Illuminate/Routing/Router.php(806): Illuminate\\Routing\\Route->run()
#28 /home/forge/villagracia.nl/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(144): Illuminate\\Routing\\Router->Illuminate\\Routing\\{closure}()
#29 /home/forge/villagracia.nl/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php(50): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#30 /home/forge/villagracia.nl/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Illuminate\\Routing\\Middleware\\SubstituteBindings->handle()
#31 /home/forge/villagracia.nl/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php(88): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#32 /home/forge/villagracia.nl/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Illuminate\\Foundation\\Http\\Middleware\\VerifyCsrfToken->handle()
#33 /home/forge/villagracia.nl/vendor/laravel/framework/src/Illuminate/View/Middleware/ShareErrorsFromSession.php(49): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#34 /home/forge/villagracia.nl/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Illuminate\\View\\Middleware\\ShareErrorsFromSession->handle()
#35 /home/forge/villagracia.nl/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php(121): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#36 /home/forge/villagracia.nl/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php(64): Illuminate\\Session\\Middleware\\StartSession->handleStatefulRequest()
#37 /home/forge/villagracia.nl/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Illuminate\\Session\\Middleware\\StartSession->handle()
#38 /home/forge/villagracia.nl/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/AddQueuedCookiesToResponse.php(37): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#39 /home/forge/villagracia.nl/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Illuminate\\Cookie\\Middleware\\AddQueuedCookiesToResponse->handle()
#40 /home/forge/villagracia.nl/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/EncryptCookies.php(75): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#41 /home/forge/villagracia.nl/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Illuminate\\Cookie\\Middleware\\EncryptCookies->handle()
#42 /home/forge/villagracia.nl/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(119): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#43 /home/forge/villagracia.nl/vendor/laravel/framework/src/Illuminate/Routing/Router.php(805): Illuminate\\Pipeline\\Pipeline->then()
#44 /home/forge/villagracia.nl/vendor/laravel/framework/src/Illuminate/Routing/Router.php(784): Illuminate\\Routing\\Router->runRouteWithinStack()
#45 /home/forge/villagracia.nl/vendor/laravel/framework/src/Illuminate/Routing/Router.php(748): Illuminate\\Routing\\Router->runRoute()
#46 /home/forge/villagracia.nl/vendor/laravel/framework/src/Illuminate/Routing/Router.php(737): Illuminate\\Routing\\Router->dispatchToRoute()
#47 /home/forge/villagracia.nl/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(200): Illuminate\\Routing\\Router->dispatch()
#48 /home/forge/villagracia.nl/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(144): Illuminate\\Foundation\\Http\\Kernel->Illuminate\\Foundation\\Http\\{closure}()
#49 /home/forge/villagracia.nl/vendor/livewire/livewire/src/Features/SupportDisablingBackButtonCache/DisableBackButtonCacheMiddleware.php(19): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#50 /home/forge/villagracia.nl/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Livewire\\Features\\SupportDisablingBackButtonCache\\DisableBackButtonCacheMiddleware->handle()
#51 /home/forge/villagracia.nl/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ConvertEmptyStringsToNull.php(27): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#52 /home/forge/villagracia.nl/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Illuminate\\Foundation\\Http\\Middleware\\ConvertEmptyStringsToNull->handle()
#53 /home/forge/villagracia.nl/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TrimStrings.php(47): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#54 /home/forge/villagracia.nl/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Illuminate\\Foundation\\Http\\Middleware\\TrimStrings->handle()
#55 /home/forge/villagracia.nl/vendor/laravel/framework/src/Illuminate/Http/Middleware/ValidatePostSize.php(27): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#56 /home/forge/villagracia.nl/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Illuminate\\Http\\Middleware\\ValidatePostSize->handle()
#57 /home/forge/villagracia.nl/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/PreventRequestsDuringMaintenance.php(110): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#58 /home/forge/villagracia.nl/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Illuminate\\Foundation\\Http\\Middleware\\PreventRequestsDuringMaintenance->handle()
#59 /home/forge/villagracia.nl/vendor/laravel/framework/src/Illuminate/Http/Middleware/HandleCors.php(49): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#60 /home/forge/villagracia.nl/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Illuminate\\Http\\Middleware\\HandleCors->handle()
#61 /home/forge/villagracia.nl/vendor/laravel/framework/src/Illuminate/Http/Middleware/TrustProxies.php(57): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#62 /home/forge/villagracia.nl/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(183): Illuminate\\Http\\Middleware\\TrustProxies->handle()
#63 /home/forge/villagracia.nl/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(119): Illuminate\\Pipeline\\Pipeline->Illuminate\\Pipeline\\{closure}()
#64 /home/forge/villagracia.nl/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(175): Illuminate\\Pipeline\\Pipeline->then()
#65 /home/forge/villagracia.nl/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(144): Illuminate\\Foundation\\Http\\Kernel->sendRequestThroughRouter()
#66 /home/forge/villagracia.nl/public/index.php(51): Illuminate\\Foundation\\Http\\Kernel->handle()
#67 {main}
"}

Donate 💰 to fund this issue

Fund with Polar

likeabas commented 5 months ago

A quick solution is to create a migration to just drop the foreign key on the imports table, but I'm reporting this issue for others who might run into it and because maybe there's a better solution. I think this should be a low priority issue.

likeabas commented 5 months ago

I have solved this in my project with the following migration, changing the foreign key to the set auth provider table, in my case 'admins':

Create a migration: php artisan make:migration change_imports_foreign_key_to_admins_table

The migration file I used, replace admins with your table name


use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        // Drop the old foreign key
        Schema::table('imports', function (Blueprint $table) {
            $table->dropForeign(['user_id']);
        });

        // Add the new foreign key
        Schema::table('imports', function (Blueprint $table) {
            $table->foreign('user_id')->references('id')->on('admins')->onDelete('cascade');
        });
    }
    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        // Drop the new foreign key
        Schema::table('imports', function (Blueprint $table) {
            $table->dropForeign(['user_id']);
        });

        // Add the old foreign key
        Schema::table('imports', function (Blueprint $table) {
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        });
    }
};
danharrin commented 5 months ago

The migration is published, so you can customize it without even creating a new migration. I don't think there is anything for us to fix here