简体   繁体   中英

Does unique constraint column needs to be added while doing upsert in laravel?

For example i have table orders in posgres DB

id order_no account_id
1 222 111
2 444 222

I have added unique constraint on order_no and account_id column. Now when I added new column and I want data to be unique on this column as well which mean the uniques values should be on (order_no,account_id,ack_id) but I do not want to add [ack_id] in unique clause at database level.

id order_no account_id ack_id
1 222 111 1
2 444 222 2

Now when i am doing upsert in laravel it throws below errors:

Invalid column reference: 7 ERROR: there is no unique or exclusion constraint matching the ON CONFLICT specification

DO i need to add ack_id in unique constraint at database level?

Below is my code which i have done for upsert: `

DB::table('order_masters')
  ->upsert($chunk, ['order_no', 'account_id','ack_id'],
     ['colums_to_update']
  );

`

I had similar error once. The error was on database level, not laravel.

The solution was by removing previous unique constraint and add new unique combo.

In your case from (order_no, account_id) to (order_no, account_id, ack_id).

Try this and hope it helps.

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