我有一个带有几个唯一索引的表。是否可以仅对其中之一进行冲突更新,对其他所有不要做,使用不要做将来的唯一索引进行处理而无需返回并修改代码或至少修改现有代码。例如:
ON CONFLICT (name, age)
DO UPDATE SET
occupation = 'teacher'
ELSE ON CONFLICT(all others) DO NOTHING;
现在,当然,除了第一个索引外,所有其他索引都会引发错误。或者,最好的处理方式是什么,这样就不会抛出错误。谢谢。
CREATE UNIQUE INDEX index1 ON contact USING btree (name, age);
CREATE UNIQUE INDEX index2 ON contact USING btree (name, address) WHERE (address IS NOT NULL);
CREATE UNIQUE INDEX index3 ...
CREATE UNIQUE INDEX index4 ...
INSERT INTO contact
(name,
age,
address,
occupation)
VALUES
('John',
25,
'1 main st',
'doctor')
ON CONFLICT (name, age)
DO UPDATE SET
occupation = 'teacher';
答案 0 :(得分:0)
避免插入该怎么办?这有点冗长,需要与您的唯一约束保持同步,但至少不应出错。
const filterTickers = (excludes = [], tickers = []) => {
const s = new Set(excludes)
return tickers.filter(t => t.split('/').every(x => !s.has(x)))
}
const result =
filterTickers(['MATIC', 'CELR'], ['BTC/USDT','ETH/BTC','CELR/BTC','FOO/CELR'])
console.log(result)
// [ "BTC/USDT", "ETH/BTC" ]