Migration errors
hondaman900 opened this issue · 8 comments
I know this seems to have been addressed in issue #35 and corrected in the package code, however after installing via composer (composer require mpociot/teamwork:^5.3
), publishing and attempting to run migration in my Laravel 5.5 project, I get the following errors:
php artisan migrate
In Connection.php line 664:
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `team_user` add constraint `team_user_user_id_foreign` foreign key (`user_id`) references `us
ers` (`id`) on delete cascade on update cascade)
In PDOStatement.php line 119:
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
In PDOStatement.php line 117:
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
After searching similar issues, I manually backed out the database changes made by the failed migration, changed increments
to bigIncrements
, but this had no effect - same error. I wasn't sure at which v.5 of Laravel that requirement kicked in, but figured it worth a shot.
Has anyone else come across this error and can recommend a solution?
You can try this migration. I just changed the bigInteger
to integer
. I'm also using Laravel 5.5
Schema::table( \Config::get( 'teamwork.users_table' ), function ( Blueprint $table )
{
$table->integer( 'current_team_id' )->unsigned()->nullable();
} );
Schema::create( \Config::get( 'teamwork.teams_table' ), function ( Blueprint $table )
{
$table->increments( 'id' )->unsigned();
$table->integer( 'owner_id' )->unsigned()->nullable();
$table->string( 'name' );
$table->timestamps();
} );
Schema::create( \Config::get( 'teamwork.team_user_table' ), function ( Blueprint $table )
{
$table->integer( 'user_id' )->unsigned();
$table->integer( 'team_id' )->unsigned();
$table->timestamps();
$table->foreign( 'user_id' )
->references( \Config::get( 'teamwork.user_foreign_key' ) )
->on( \Config::get( 'teamwork.users_table' ) )
->onUpdate( 'cascade' )
->onDelete( 'cascade' );
$table->foreign( 'team_id' )
->references( 'id' )
->on( \Config::get( 'teamwork.teams_table' ) )
->onDelete( 'cascade' );
} );
Schema::create( \Config::get( 'teamwork.team_invites_table' ), function(Blueprint $table)
{
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->integer('team_id')->unsigned();
$table->enum('type', ['invite', 'request']);
$table->string('email');
$table->string('accept_token');
$table->string('deny_token');
$table->timestamps();
$table->foreign( 'team_id' )
->references( 'id' )
->on( \Config::get( 'teamwork.teams_table' ) )
->onDelete( 'cascade' );
});
as @luigel stated this is a problem with primary keys in older Laravel projects. Since Larvel v5.8 bigIncrement is used for these columns (laravel/laravel@426df7a).
We dropped support for Laravel 5.8 and lower and updated our migrations to meet the standard usages.
hi @hondaman900
the correct command is composer require mpociot/teamwork: ^ 5.2
, I'm was wrong with the previous one. The relevant tests have already been done and the migrations are carried out correctly.
Hi @oliuz
Using composer require mpociot/teamwork: ^ 5.2
I get
[InvalidArgumentException]
Could not find package 5.2.
Did you mean one of these?
level-2/dice
milon/barcode
lusitanian/oauth
hipsterjazzbo/landlord
backpack/backupmanager
Is version 5.2 removed...?
Is there perhaps a version of your fork of Teamwork for Laravel 5.5?
If you need exact 5.2 you could try to install it by its full version name:
composer require mpociot/teamwork:5.2.0
Awesome - that worked without any issues. Thank you all for the great help!
Now, at a higher level, I need to strategize on a plan for upgrading my app from Laravel 5.5 -> 7 (or 8). That's for another day....
Wanted to followup on this because after upgrading my app (a pre Teamwork version) to Laravel 6, I then thought the mociot/teamwork package would install out-of-the-box. After all the issue we discussed above are no longer valid in Laravel 6, right?
Well, I get the same errors again with the stock download and my Laravel 6 project.
php artisan migrate
Migrating: 2020_08_01_043041_teamwork_setup_tables
In Connection.php line 669:
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint (SQL: alter table `team_user` add constraint
`team_user_user_id_foreign` foreign key (`user_id`) references `users` (`id`) on delete cascade on update cascade)
In PDOStatement.php line 129:
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
In PDOStatement.php line 127:
SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
So now I think the issue is that my original user tables which were built in Laravel 5.5, before I upgraded the project, don't match field types for the foreign keys this migration is trying to implement. Guess I need to manually build the tables and relationships to overcome this issue.
Yup, manually deleting changes the failed migration made to the database before crapping out, changing the two bigInteger
references to integer
and running the migration again worked.