我在DB中有一个表:
<createTable tableName="api_consumer">
<column name="id"
type="INTEGER">
<constraints primaryKey="true"
primaryKeyName="PK_api_consumer_id"/>
</column>
<column name="business_id"
type="VARCHAR(50)"/>
<column name="type"
type="VARCHAR(32)"/>
<column name="status"
type="VARCHAR(32)"/>
<column name="description"
type="VARCHAR(256)"/>
<column name="created_by"
type="VARCHAR(64)"/>
<column name="created_date"
type="DATETIME"/>
<column name="modified_by"
type="VARCHAR(64)"/>
<column name="modified_date"
type="DATETIME"/>
<!--TTP below-->
<column name="name"
type="VARCHAR(64)">
</column>
<column name="origin_country"
type="VARCHAR(64)"/>
<column name="license_authority_name"
type="VARCHAR(64)"/>
<column name="registration_date"
type="DATETIME"/>
<column name="api_callback_url"
type="VARCHAR(256)"/>
<column name="application_base_url"
type="VARCHAR(256)"/>
<column name="authorization_callback_url"
type="VARCHAR(256)"/>
<column name="address"
type="INTEGER">
<constraints foreignKeyName="FK_api_consumer_address_id"
references="address(id)"/>
</column>
<column name="local_address"
type="INTEGER">
<constraints foreignKeyName="FK_api_consumer_local_address_id"
references="address(id)"/>
</column>
<column name="license"
type="INTEGER">
<constraints foreignKeyName="FK_api_consumer_license_id"
references="license(id)"/>
</column>
</createTable>
以SQL方言生成:
CREATE TABLE api_consumer
(
id INTEGER NOT NULL
CONSTRAINT "PK_api_consumer_id"
PRIMARY KEY,
business_id VARCHAR(50),
type VARCHAR(32),
status VARCHAR(32),
description VARCHAR(256),
created_by VARCHAR(64),
created_date TIMESTAMP,
modified_by VARCHAR(64),
modified_date TIMESTAMP,
name VARCHAR(64),
origin_country VARCHAR(64),
license_authority_name VARCHAR(64),
registration_date TIMESTAMP,
api_callback_url VARCHAR(256),
application_base_url VARCHAR(256),
authorization_callback_url VARCHAR(256),
address INTEGER
CONSTRAINT FK_api_consumer_address_id
REFERENCES address,
local_address INTEGER
CONSTRAINT FK_api_consumer_local_address_id
REFERENCES address,
license INTEGER
CONST
RAINT FK_api_consumer_license_id 参考许可证 );
从IntelliJ DB控制台我试图将值插入此表并在其上测试我的触发器。 我的插入内容如下:
INSERT INTO api_consumer VALUES ('11', '101', 'PSD2', 'NEW', 'TEST OF TRIGGER ON INSERT', 'kkwiatkowski', CURRENT_TIMESTAMP, null, null, 'SPRAWDZAM_COS', 'PL', null, null, null, null, null, 1, 1, 1);
我收到的错误如下:
sql> INSERT INTO api_consumer VALUES ('11', '101', 'PSD2', 'NEW', 'TEST OF TRIGGER ON INSERT', 'kkwiatkowski', CURRENT_TIMESTAMP, null, null, 'SPRAWDZAM_COS', 'PL', null, null, null, null, null, 1, 1, 1)
[2017-10-16 10:30:43] [42703] ERROR: column "insert" does not exist
[2017-10-16 10:30:43] Gdzie: PL/pgSQL function proc_subscription_audit_insert() line 3 at SQL statement
单词:“Gdzie”表示在英语中的位置。
如果我删除此id value: '11'
,则插入查询无效,导致参数范围向右移动。可以理解的。
[2017-10-16 10:39:08] [22007] ERROR: invalid input syntax for type timestamp: "SPRAWDZAM_COS"
[2017-10-16 10:39:08] Pozycja: 133
单词:“Pozycja”表示在英语中的位置。
1)那我该如何解决?
2)如何在此处实现自动增量值?
我尝试在xml结构中添加autoIncrement="true"
,然后在没有id
参数的情况下调用此插入查询。这也不会将nextval(id)
添加到插入查询中。
修改
procedures.xml
<changeSet id="1.0-procedures" author="blab">
<sql>
DROP FUNCTION IF EXISTS proc_api_consumer_audit_insert();
DROP FUNCTION IF EXISTS proc_api_consumer_audit_update();
DROP FUNCTION IF EXISTS proc_subscription_audit_insert();
DROP FUNCTION IF EXISTS proc_subscription_audit_update();
</sql>
<createProcedure>
CREATE FUNCTION proc_api_consumer_audit_insert()
RETURNS TRIGGER AS $api_consumer$
BEGIN
INSERT INTO api_consumer_audit(api_consumer_id, change_type, changed_by, changed_date, business_id_old, business_id_new, name_old, name_new, api_callback_url_old, api_callback_url_new, application_base_url_old, application_base_url_new, authorization_callback_url_old, authorization_callback_url_new, status_old, status_new) VALUES(NEW.id, 'INSERT', CURRENT_USER, CURRENT_TIMESTAMP, null, NEW.business_id, null, NEW.name, null, NEW.api_callback_url, null, NEW.application_base_url, null, NEW.authorization_callback_url, null, NEW.status);
RETURN NEW;
END;
$api_consumer$ LANGUAGE plpgsql;
</createProcedure>
<createProcedure>
CREATE FUNCTION proc_api_consumer_audit_update()
RETURNS TRIGGER AS $api_consumer$
BEGIN
INSERT INTO api_consumer_audit(api_consumer_id, change_type, changed_by, changed_date, business_id_old, business_id_new, name_old, name_new, api_callback_url_old, api_callback_url_new, application_base_url_old, application_base_url_new, authorization_callback_url_old, authorization_callback_url_new, status_old, status_new) VALUES(NEW.id, 'UPDATE', CURRENT_USER, CURRENT_TIMESTAMP, OLD.business_id, NEW.business_id, OLD.name, NEW.name, OLD.api_callback_url, NEW.api_callback_url, OLD.application_base_url, NEW.application_base_url, OLD.authorization_callback_url, NEW.authorization_callback_url, OLD.status, NEW.status);
RETURN NEW;
END;
$api_consumer$ LANGUAGE plpgsql;
</createProcedure>
<createProcedure>
CREATE FUNCTION proc_subscription_audit_insert()
RETURNS TRIGGER AS $subscription_audit$
BEGIN
INSERT INTO subscription_audit(subscription_id, change_type, status_old, status_new, valid_from_old, valid_from_new, valid_through_old, valid_through_new, changed_by, changed_date) VALUES (NEW.id, INSERT, null, NEW.status, null, NEW.valid_from, null, NEW.valid_through, CURRENT_USER, CURRENT_TIMESTAMP);
RETURN NEW;
END;
$subscription_audit$ LANGUAGE plpgsql;
</createProcedure>
<createProcedure>
CREATE FUNCTION proc_subscription_audit_update()
RETURNS TRIGGER AS $subscription_audit$
BEGIN
INSERT INTO subscription_audit(subscription_id, change_type, status_old, status_new, valid_from_old, valid_from_new, valid_through_old, valid_through_new, changed_by, changed_date) VALUES (NEW.id, 'UPDATE', OLD.status, NEW.status, OLD.valid_from, NEW.valid_from, OLD.valid_through, NEW.valid_through, CURRENT_USER, CURRENT_TIMESTAMP);
RETURN NEW;
END;
$subscription_audit$ LANGUAGE plpgsql;
</createProcedure>
<rollback>
DROP FUNCTION IF EXISTS proc_api_consumer_audit_insert();
DROP FUNCTION IF EXISTS proc_api_consumer_audit_update();
DROP FUNCTION IF EXISTS proc_subscription_audit_insert();
DROP FUNCTION IF EXISTS proc_subscription_audit_update();
</rollback>
</changeSet>
triggers.xml
<changeSet id="1.0-triggers" author="blab">
<sql>
DROP TRIGGER IF EXISTS trg_api_consumer_audit_insert ON api_consumer;
DROP TRIGGER IF EXISTS trg_api_consumer_audit_update ON api_consumer;
<!-- DROP TRIGGER IF EXISTS trg_api_consumer_audit_delete ON api_consumer-->
DROP TRIGGER IF EXISTS trg_subscription_audit_insert ON subscription;
DROP TRIGGER IF EXISTS trg_subscription_audit_update ON subscription;
<!-- DROP TRIGGER IF EXISTS trg_subscription_audit_delete ON subscription-->
</sql>
<createProcedure>
CREATE TRIGGER trg_api_consumer_audit_insert
AFTER INSERT ON api_consumer
FOR EACH ROW EXECUTE PROCEDURE proc_api_consumer_audit_insert();
</createProcedure>
<createProcedure>
CREATE TRIGGER trg_api_consumer_audit_update
AFTER UPDATE ON api_consumer
FOR EACH ROW EXECUTE PROCEDURE proc_api_consumer_audit_update();
</createProcedure>
<createProcedure>
CREATE TRIGGER trg_subscription_audit_insert
AFTER INSERT ON api_consumer
FOR EACH ROW EXECUTE PROCEDURE proc_subscription_audit_insert();
</createProcedure>
<createProcedure>
CREATE TRIGGER trg_subscription_audit_update
AFTER UPDATE ON api_consumer
FOR EACH ROW EXECUTE PROCEDURE proc_subscription_audit_update();
</createProcedure>
<rollback>
DROP TRIGGER IF EXISTS trg_api_consumer_audit_insert;
DROP TRIGGER IF EXISTS trg_api_consumer_audit_update;
DROP TRIGGER IF EXISTS trg_subscription_audit_insert;
DROP TRIGGER IF EXISTS trg_subscription_audit_update;
</rollback>
</changeSet>