Unnikrishnan
Unnikrishnan

Reputation: 3313

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'

Upvotes: 0

Views: 914

Answers (2)

duy nguyen
duy nguyen

Reputation: 79

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

Upvotes: 1

Ali Özen
Ali Özen

Reputation: 1609

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

Upvotes: 0

Related Questions