如何推迟Postgres

时间:2017-04-11 14:53:36

标签: sql postgresql go deferrable-constraint

我尝试设置一个可以推迟的外键约束,以便在插入查找/数据透视表到事务结束时不会检查它。但是,它在psql shell中运行,但它只是在代码中不起作用。与psql shell中的相同,我也在代码中启动了begin的事务。

这是sql:

create table campaign_r_company (
  campaign_id         uuid            not null references campaign(id) on delete cascade deferrable initially deferred,
  company_id          varchar(32)     not null,
  primary key (campaign_id, company_id)
);

以下是代码:

  tx, err := d.Begin()
  if err != nil {
    return err
  }


  err = h(tx) // there are two db queries will be called in this function

  if err == nil {
    err = tx.Commit()
  }

H(TX):

_, err := cxt.Exec(fmt.Sprintf(`INSERT INTO hp_campaign (%s) VALUES (%s)`, proplist("", campaignProps), arglist(1, len(campaignProps))),
    id, v.Name, created, v.Updated,
)
if err != nil {
    return err
}

v.Id = id
v.Created = created

if (opts & StoreOptionStoreRelated) == StoreOptionStoreRelated {
    err := d.attach("company", "campaign_r_company", v.Companies, v.Id)
    if err != nil {
        return err
    }

}

连接():

func (d *Database) attach(entityName string, tableName string, ids []string, campaignID string) error {

    for _, id := range ids {

        stmt := fmt.Sprintf(`INSERT INTO %s (%s) VALUES ($1, $2)`, tableName, fmt.Sprintf("campaign_id, %s_id", entityName))
        _, err := d.db.Exec(stmt, campaignID, id)

        if err != nil {
            return err
        }
    }
    return nil

}

错误:

insert or update on table "campaign_r_company" violates foreign key constraint "campaign_r_company_campaign_id_fkey"

2 个答案:

答案 0 :(得分:0)

如果您不使用手动事务管理,Go + PG会为您执行此操作。在这种情况下,任何语句都是单个事务,并在每个事务结束时检查约束。因此引发了异常。

答案 1 :(得分:0)

从更新的代码和后续评论中,我们现在知道问题是这两个查询是分开执行的,而不是在单个事务中执行。