You could use SQLite's upsert syntax to find these colliding values.
Try something like this, maybe?
insert into a_table (id1, id2)
select ... whatever ...
ON CONFLICT() DO UPDATE
SET id1=excluded.id1 || '-dup' || RANDOM,
SET id2=excluded.id2 || '-dup' || RANDOM;
(Not sure I have the syntax exactly right.) This will tag your duplicate values with -dup123456
like tags. This only works if id1
and id2
are text.
But, if these are your only two columns and together their values are the PK, you can simply ignore the issue by saying ON CONFLICT() DO NOTHING
. If your application's integrity won't suffer by ignoring those dups.
This syntax was added to SQLite with version 3.24.0 (2018-06-04).
Edit you can run a separate SELECT to detect the collisions before you attempt the update. Maybe something like this will help. Note my use of the WITH common table expression as a way to make the query clearer.
WITH newkeys AS (
select d_table.id1,
'1' as id2
from b_table
join c_table on b_table.id = c_table.path_id
join d_table on c_table.id = d_table.id1
left join a_table on d_table.id1 = a_table.id1
where a_table.id1 is null
and b_table.path like 'X:\%'
)
SELECT COUNT(*) number_of_duplicates_in_a_table,
a_table.id1, a_table.id2
FROM a_table
JOIN newkeys
ON a_table.id1 = newkeys.id1
AND b_table.id2 = newkeys.id2
GROUP BY a_table.id1, a_table.id2