SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in you r SQL syntax;

Published 3 years ago by bizhanmp

i have to migration : 1-create_countries_table " Schema::create('countries', function(Blueprint $table) { $table->increments('id'); $table->string('name'); $table->timestamps(); }); " 2-create_cities_table Schema::create('cities', function(Blueprint $table) { $table->increments('id'); $table->string('name'); $table->smallInteger('country_id'); $table->timestamps(); $table->foreign('country_id')->refrences('id')->on('countries'); }); when i use php artisan migrate , i see thi error "[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 cities add constraint cities_country_id_foreign foreign key (country_id) references countries ())" What is the problem?

Best Answer (As Selected By bizhanmp)
bizhanmp

I found my problem . i must use $table->integer('country_id')->unsigned(); because every increments determine int(10) by Laravel and just this foreign key definition is mach with int(10).

RachidLaasri

Put your code between ``` to style it, it's hard to read.

bizhanmp

in cities i have $table->foreign('country_id')->refrences('id')->on('countries'); and i get his error " [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 cities add constraint cities_country_id_foreign foreign key (country_id) references countries ()) "

frezno
frezno
3 years ago (119,540 XP)

you have a typo. it should be references not refrences:

$table->foreign('country_id')->references('id')->on('countries');
bizhanmp

I found my problem . i must use $table->integer('country_id')->unsigned(); because every increments determine int(10) by Laravel and just this foreign key definition is mach with int(10).

arielcalcano

@bizhanmp Thanks a lot, that was my problem too.

Please sign in or create an account to participate in this conversation.