我正在查看7/01为 PostgreSQL安排的commit-fest,我看到 Pg 可能很快就会获得“身份列”。
我在information_schema.columns 中发现了一些提及但没什么
is_identity yes_or_no Applies to a feature not available in PostgreSQL
identity_generation character_data Applies to a feature not available in PostgreSQL
identity_start character_data Applies to a feature not available in PostgreSQL
identity_increment character_data Applies to a feature not available in PostgreSQL
identity_maximum character_data Applies to a feature not available in PostgreSQL
identity_minimum character_data Applies to a feature not available in PostgreSQL
identity_cycle yes_or_no Applies to a feature …Run Code Online (Sandbox Code Playgroud) 我在一张桌子上有 2 个触发器;一种适用于插入:
CREATE TRIGGER "get_user_name"
AFTER INSERT ON "field_data"
FOR EACH ROW EXECUTE PROCEDURE "add_info"();
Run Code Online (Sandbox Code Playgroud)
这会更新表中的一些值。
还有一个用于更新(填充历史表):
CREATE TRIGGER "set_history"
BEFORE UPDATE ON "field_data"
FOR EACH ROW EXECUTE PROCEDURE "gener_history"();
Run Code Online (Sandbox Code Playgroud)
问题是,当我在表中插入新行时,该过程"add_info"()会进行更新并因此触发第二个触发器,该触发器以错误结束:
Run Code Online (Sandbox Code Playgroud)ERROR: record "new" has no field "field1"
我怎样才能避免这种情况?
这是我建议的架构:
CREATE TABLE Surveys (
id serial primary key,
user_email citext,
survey_data jsonb,
created_at timestamp default current_timestamp
);
CREATE INDEX surveys_email_idx ON Surveys(user_email);
CREATE USER SurveyWriter;
Run Code Online (Sandbox Code Playgroud)
我知道我需要:
GRANT INSERT ON dbname.Surveys TO SurveyWriter;
Run Code Online (Sandbox Code Playgroud)
但我还需要:
GRANT INSERT, UPDATE ON dbname.surveys_email_idx to SurveyWriter;
Run Code Online (Sandbox Code Playgroud)
还有什么我没有想到的吗?
在 Postgres 10 中创建序列时,如何在更新时自动递增?(不仅仅是为下一个插入的行分配下一个更高的数字。)
例如,假设我创建了在此页面上找到的下表和序列:
CREATE TABLE fruits(
id SERIAL PRIMARY KEY,
name VARCHAR NOT NULL
);
INSERT INTO fruits(name) VALUES('Orange');
INSERT INTO fruits(id,name) VALUES(DEFAULT,'Apple');
SELECT * FROM fruits;
id | name
----+--------
1 | Apple
2 | Orange
(2 rows)
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,它在插入时正确地自动将“id”列加一。但是,如果我进行如下更新:
update fruits
set name = 'Orange2'
where name = 'Orange';
SELECT * FROM fruits;
id | name
----+--------
1 | Apple
How do I get this to auto-increment to 3? --> 2 | Orange2
(2 …Run Code Online (Sandbox Code Playgroud)