Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

Your code shoudl be:

$t->integer('format_id')->unsigned();
$t->foreign('format_id')->reference('id')->on('scene_formats');  

you are using a dot "." when you need "->" for the unsigned fuction.

Quick observation, you can't add nullable() because is a FK.

$t->integer('format_id')->unsigned()->nullable();

Suggestion, try to name your tables in a plural like:

Schema::create('formats', function (Blueprint $t) {

});

When using pivot tables then you can use table1_table2

Last updated 8 years ago.
0

Thanks for pointing out the chaining issue and for the suggestion.

Still, the foreign key is still not being set. Code:

    Schema::create('scenes_formats', function (Blueprint $table) {
        $table->engine = 'InnoDB'; 
        $table->increments('id');
        $table->timestamps();
    }); 
    Schema::create('scenes', function (Blueprint $t) {
        $t->engine = 'InnoDB'; 
        $t->increments('id');
        $t->timestamps();
        $t->integer('format_id')->unsigned();
        $t->foreign('format_id')->reference('id')->on('scenes_formats');    
    }); 

Error Message " [Illuminate\Database\QueryException] SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1 (SQL: alter table scenes add constraint scenes_format_id_foreign foreign key (format_id) references scenes_formats ())

[PDOException] SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1"

MySQL version 5.7.9

Last updated 8 years ago.
0
Solution

Got it to work finally.

Had to separate it into another step. Code:

    Schema::create('scenes_formats', function (Blueprint $table) {
        $table->engine = 'InnoDB'; 
        $table->increments('id');
        $table->timestamps();
    }); 
    Schema::create('scenes', function (Blueprint $t) {
        $t->engine = 'InnoDB'; 
        $t->increments('id');
        $t->timestamps();
        $t->integer('format_id')->unsigned();
    }); 
    Schema::table('scenes', function($table) {
        $table->foreign('format_id')->references('id')->on('scenes_formats');
    }); 
Last updated 8 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

prezjt prezjt Joined 27 Sep 2016

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.