I am using laravel's artisan command to create a database table with a foreign key. I'm getting the following error when I run the php artisan migrate
command:
SQLSTATE[23000]: Integrity constraint violation: 1022 Can't write; duplicate key in table '#sql-73_ca' (SQL: alter table
table_1
add constrainttable_1_table_1_sid_foreign
foreign key
(table_1_sid
) referencestable_2
(id
))
Also, I don't have a table '#sql-73_ca'
-
1share your migration snippets .Sark– Sark2018年01月04日 10:13:26 +00:00Commented Jan 4, 2018 at 10:13
-
Could you please post the code of all relevant migrations?Brotzka– Brotzka2018年01月04日 10:13:32 +00:00Commented Jan 4, 2018 at 10:13
-
can you plz share your both table migrations.Ritesh Khatri– Ritesh Khatri2018年01月04日 10:36:42 +00:00Commented Jan 4, 2018 at 10:36
2 Answers 2
if you using phpmyadmin you should copy a section of error notification to the query sql in phpmyadmin:
" alter table table_1 add constraint table_1_table_1_sid_foreign
foreign key (table_1_sid) references table_2 (id)"
It will display more information for error
Comments
Its about unique() command in your migration file. You should delete this if you dont wanna get error. Or add a function for if there is unique column. You should share your migration. We cant understand with this.
Also you can try this in related controller.
try { //codes
} catch(\Illuminate\Database\QueryException $e){
$errorCode = $e->errorInfo[1];
if($errorCode == '1062'){
echo('Error Message');
}
}