Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

bizhanmp's avatar

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

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?

0 likes
5 replies
RachidLaasri's avatar

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

2 likes
bizhanmp's avatar

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's avatar

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

$table->foreign('country_id')->references('id')->on('countries');
6 likes
bizhanmp's avatar
bizhanmp
OP
Best Answer
Level 1

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).

1 like

Please or to participate in this conversation.