我有一个名为operator
的下表,在下面的v10数据库中定义。我想基于具有以下签名的函数来向上插入该表。 JSONB参数可能具有表中的任何字段,也可能没有。 (请注意,username
或primary_email
不能为空,并且是备用键,因此(应该)始终存在于JSON对象中,我将其用于任何更常用的ON CONFLICT ON CONSTRAINT operator_primaryemail_uidx
类型upsert。 )我有什么选择呢?
CREATE OR REPLACE FUNCTION upsert_operator(_data JSONB DEFAULT NULL)
Example JSON: {
"username": "potato",
"last_name": "doe",
"street": "123 Main St."
}
CREATE TABLE IF NOT EXISTS operator (
id UUID DEFAULT uuid_generate_v1mc(),
party_id UUID,
username VARCHAR(32) NOT NULL,
profile_picture_uri VARCHAR(512),
first_name VARCHAR(64),
last_name VARCHAR(64),
street VARCHAR(128),
specifier VARCHAR(128),
city VARCHAR(64),
state VARCHAR(2),
zipcode VARCHAR(9),
primary_email CITEXT NOT NULL,
primary_phone VARCHAR(10),
secondary_email CITEXT,
secondary_phone VARCHAR(10),
is_locked BOOLEAN NOT NULL DEFAULT false,
must_reset_pwd BOOLEAN NOT NULL DEFAULT false,
retry_count SMALLINT DEFAULT 0,
signed_eula BOOLEAN NOT NULL DEFAULT false,
last_login TIMESTAMP WITH TIME ZONE,
created TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT statement_timestamp(),
updated TIMESTAMP WITH TIME ZONE DEFAULT 'infinity',
deleted TIMESTAMP WITH TIME ZONE DEFAULT 'infinity',
-- keys
CONSTRAINT operator_pk PRIMARY KEY (id),
CONSTRAINT operator_party_fk FOREIGN KEY (party_id)
REFERENCES party(id) MATCH SIMPLE
ON UPDATE RESTRICT ON DELETE RESTRICT,
-- checks
CONSTRAINT operator_username_ak UNIQUE(username),
CONSTRAINT operator_primaryemail_uidx UNIQUE(primary_email),
CONSTRAINT operator_updated_gt_created CHECK (updated >= created),
CONSTRAINT operator_deleted_gt_created CHECK (deleted >= created)
)