简体   繁体   中英

Laravel migration error on creating table with foreign key

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 constraint table_1_table_1_sid_foreign foreign key
( table_1_sid ) references table_2 ( id ))

Also, I don't have a table '#sql-73_ca'

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

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');
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM