我想存储加密的密钥,并且我想确保此 SQLAlchemy 模型/表中始终没有条目或只有一个条目。如何执行?
使用一些必须是唯一的生成内容。像这样的东西:
CREATE TABLE t1(id int GENERATED ALWAYS AS IDENTITY PRIMARY KEY
, counter INT UNIQUE GENERATED ALWAYS AS ( 1 ) STORED
, content TEXT);
INSERT INTO t1(content) VALUES ('my content') RETURNING id;
INSERT INTO t1(content) VALUES ('some other content') RETURNING id; -- fail
DELETE FROM t1 WHERE content = 'my content';
INSERT INTO t1(content) VALUES ('some other content') RETURNING id;