【问题标题】:Trigger after insert, update a related model Postgresql插入后触发,更新相关模型Postgresql
【发布时间】:2015-05-29 02:40:57
【问题描述】:

这是我第一次创建触发器,所以我有点困惑。我正在关注this guide

这是我到目前为止所做的:

DROP TRIGGER IF EXISTS "update_metas" ON "post";
CREATE TRIGGER "update_metas"
    AFTER INSERT ON "post"
    FOR EACH ROW EXECUTE PROCEDURE update_post_count();

我有两张桌子:userpost。我需要做的是为每个新创建的post 增加列user.postCount。外键是post.user_id

我正在创建的程序如下:

CREATE FUNCTION update_post_count() RETURNS TRIGGER AS $updates_user_postCount$
    BEGIN
        -- I know that NEW contains the new post info, so I
        -- can gather the user_id by doing NEW.post_id.
        -- 
        -- What exactly should I do here?
        RETURN NEW;
    END;
$updates_user_postCount$ LANGUAGE plpgsql;

我应该如何构建这个程序?我可以直接使用SQL 查询吗,例如:

UPDATE "user"
    SET "user"."post_count" = "user"."post_count" + 1
    WHERE "user"."_id" = NEW.idol_id;

更新

我尝试在过程中使用该 SQL 语句,但它返回错误 error: column "user" of relation "user" does not exist

这是我用来创建userpost 表的SQL 语句:

CREATE TABLE IF NOT EXISTS "user" (
    _id BIGSERIAL UNIQUE,
    __id TEXT UNIQUE,
    fbid VARCHAR(100),
    name VARCHAR(100) NOT NULL,
    email VARCHAR(100) NOT NULL UNIQUE,
    password VARCHAR(512) NOT NULL,
    profile_picture VARCHAR(512),
    profile_cover VARCHAR(512),
    profile_about TEXT,
    profile_birth_date BIGINT,
    social_facebook VARCHAR(256),
    social_twitter VARCHAR(256),
    social_instagram VARCHAR(256),
    post_count BIGINT,
    highlighted BOOLEAN,
    idol BOOLEAN,
    free BOOLEAN,
    blocked BOOLEAN
);

CREATE TABLE IF NOT EXISTS "post" (
    _id BIGSERIAL UNIQUE,
    __id TEXT UNIQUE,
    idol_id BIGINT,
    removed BOOLEAN,
    free BOOLEAN,
    created_at BIGINT,
    hashtags VARCHAR(1024),
    audio_src VARCHAR(512),
    audio_size INTEGER,
    audio_length INTEGER,
    FOREIGN KEY ("idol_id") REFERENCES "user"("_id")
);

【问题讨论】:

    标签: postgresql


    【解决方案1】:

    您的触发函数基本正确。唯一的问题是UPDATE 语句不能使用table.column 表示法。

    来自documentation不要在目标列的规范中包含表的名称——例如,UPDATE tab SET tab.col = 1 是无效的。

    CREATE FUNCTION update_post_count() RETURNS TRIGGER AS $updates_user_postCount$
    BEGIN
      UPDATE "user"
      SET "post_count" = "post_count" + 1
      WHERE "_id" = NEW.idol_id;
      RETURN NEW;
    END;
    $updates_user_postCount$ LANGUAGE plpgsql;
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-24
    • 2021-03-08
    • 1970-01-01
    • 1970-01-01
    • 2016-10-13
    • 1970-01-01
    • 1970-01-01
    • 2015-11-06
    相关资源
    最近更新 更多