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
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
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');
});
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community