在我的架构中,我有一个带
的表id | word | count | remote_id
-----------------------------
我基本上喜欢这样做是因为我在这个表中插入了行:
insert into table (word, count, remote_id) VALUES ('cat', 10, 2);
首先,如果' cat'已经存在remote_id = 2,我喜欢' cat'的最终计数值为10 +它的现有价值。
如果它不存在,它将照常插入。
我怎样才能做到这一点?
此外,我现在有一个断言,列字必须是唯一的,但我只是喜欢每个remote_id唯一。
答案 0 :(得分:1)
您需要python manage.py migrate
列吗?如果您可以将对id
放入主键,则可以使用on duplicate key
syntax
word, remote_id
编辑:我的立场得到了纠正。即使您没有主键,也可以使用此语法,只要您在insert into table (word, count, remote_id) VALUES ('cat', 10, 2)
on duplicate key update count = count +values(count);
对上有唯一索引,这就是您希望强制执行该条件。
答案 1 :(得分:0)
在架构中,将唯一约束添加到两列
Alter table table_name add unique 'uni_undex' (word, remote_id)
编辑: 为了计数 您可以在plsql中插入事件后编写触发器 或者选择+(更新或插入)