I have a locally developed laravel project with sqlite. For deployment reasons I want to switch to mysql. Unfortunately my relation migrations do not work anymore and produce the following error (I have made sure the order in which they run is correct, all other required tables are generated first and look correct)
Can't create table `laraveltest`.`test1s_test2s` (errno: 150 "Foreign key constraint is incorrectly formed")
(SQL: alter table `test1s_test2s` add constraint `test1s_test2s_test1_id_foreign` foreign key (`suacap_id`)
references `test1s` (`id`) on delete cascade)
The migration looks like this:
test1
public function up()
{
Schema::create('test1s', function (Blueprint $table) {
$table->id();
...
test2
public function up()
{
Schema::create('test2s', function (Blueprint $table) {
$table->id();
...
relation table test1s_test2s
public function up()
{
Schema::create('test1s_test2s', function (Blueprint $table) {
$table->primary(['test1_id', 'test2_id']);
$table->string('test1_id');
$table->foreign('test1_id')
->references('id')
->on('test1s')->onDelete('cascade');
$table->string('test2_id');
$table->foreign('test2_id')
->references('id')
->on('test2s')->onDelete('cascade');
});
}
I'm guessing this is related to the primary keys not being unsigned while the bigInt id's of the other tables are? I tried modifying
$table->primary(['test1_id', 'test2_id'])->unsigned();
but that does not work.
Can someone point me in the right direction? Thanks
CodePudding user response:
Think whenever u make something Foreign keys they should be UNSIGNED BIGINT not string , in Laravel 9 - $table->foreignId('user_id'); also read the official document Laravel Official Doc