Using Laravel 5.1 and PostgreSQL, by necessity, I need to insert rows in the middle of the table.
The id
s are primary keys, but are not autoincremented. Instead, the id
s are set in the loaded spreadsheet, which map to other values.
Before I insert, I was thinking I could pre-offset the ids then insert. The problem with this is, offsetting row id 2
to id 3
obviously will throw the following error because id 3
already exists:
duplicate key value violates unique constraint
Question: I'm looking to ignore the pkey constraint while I offset the IDs.
Update Query:
update table1 as t1
set id = id + 1
where id >= 2;
table1
id | str_a
----------
1 a
2 b
3 c
table1 Modified: first offset
id | str_a
----------
1 a
3 b <--- offset id by +1
4 c <--- offset id by +1
table1 Modified: Inserted {id: 2, str_a: 'hello world'}
id | str_a
----------
1 a
2 hello world <--- inserted
3 b
4 c
table1_table2
table1_id | table2_id
--------------------
1 1
2 2
3 3
table1_table2 Modified: After insert
table1_id | table2_id
--------------------
1 1
2 null
3 <- offset 2 <- table2_id 2 now belongs to offset table1_id 3
4 <- offset 3 <- table2_id 3 now belongs to offset table1_id 4
table2
id | str_b
----------
1 d
2 e
3 f
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire