Level 51
@dengdeng Did you add
$table->integer('phone_id')->unsigned();
https://laravel.com/docs/5.2/migrations#foreign-key-constraints
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I have one migration to execute. And here is my code:
public function up()
{
Schema::table('students', function (Blueprint $table) {
//
$table->foreign('phone_id')->reference('id')->on('phone');
});
}
where i execute migration, it said i have an error in my sql. And i run php artisan migrate --pretend to output the sql:
alter table `students` add constraint `students_phone_id_foreign` foreign key (`phone_id`) references `phone` ()
As you can see, there is a () at the end of the sql. How does it come out?
@dengdeng I think its references('id'), not reference('id'). And that should be "phones"-table, not "phone"-table. Unless you table has just one phone hard-coded into first row.
Also, the actual sql error message could help here. Just a suggestion for the future.
Please or to participate in this conversation.