【问题标题】:Upsert postgresql returning idUpsert postgresql 返回 id
【发布时间】:2015-11-17 03:06:17
【问题描述】:

最初我认为这是一个 heroku 问题,由于某种奇怪的原因,这段代码在本地运行良好,但经过进一步调查,我意识到 id 一直返回 0。本质上,我正在尝试编写一个返回 id 的 upsert。我正在使用 sql 库。

-- ----------------------------
--  Table structure for books
-- ----------------------------
DROP TABLE IF EXISTS "public"."books" CASCADE;
CREATE TABLE "public"."books" (
    "id" serial primary key,
    "title" varchar(255) NOT NULL COLLATE "default",
    "first" varchar(40) NOT NULL COLLATE "default",
    "last" varchar(40) NOT NULL COLLATE "default",
    "class" varchar(40) NOT NULL COLLATE "default"
)
WITH (OIDS=FALSE);
-- ----------------------------
--  Table structure for bookitems
-- ----------------------------
DROP TABLE IF EXISTS "public"."bookitem";
CREATE TABLE "public"."bookitem" (

    "id" serial primary key,
    "price" int NOT NULL,
    "condition" int NOT NULL,
    "views" int NOT NULL,
    "seller" bigint NOT NULL,
    "book" int REFERENCES books(id),
    "saletype" int NOT NULL,
    "date" bigint NOT NULL,
    "description" varchar(255) NOT NULL COLLATE "default",
    "bucket" int NOT NULL,
    "status" int NOT NULL --This is for showing what the staus of the item is (bought sold etc.)
    --FOREIGN KEY (book) REFERENCES books(id)
)
WITH (OIDS=FALSE);

这里是触发错误的代码:

rows, err := db.Query(`with vals as (
      select $1::VARCHAR as title, $2::VARCHAR as first, $3::VARCHAR as last, $4::VARCHAR as class
    )
    insert into books (title, first, last, class)
    select v.title, v.first, v.last, v.class
    from vals as v
    where not exists (select * from books as t where t.title = v.title and t.last = v.last and t.first = v.first and t.class = v.class)
    RETURNING id`, r.FormValue("title"),
            r.FormValue("first"),
            r.FormValue("last"),
            r.FormValue("class"))
        //defer rows.Close()
        rows.Scan(&id)

        PanicIf(err)

        _, err = db.Query("INSERT INTO bookitem (price, condition, views, seller, book, saletype,   date, description, bucket, status) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)",
            r.FormValue("price"),
            r.FormValue("condition"),
            0,
            0,
            id,
            r.FormValue("saletype"),
            0,
            r.FormValue("description"),
            0,
            0)

        PanicIf(err)

我运行它部署到heroku时遇到的错误是:

pq: insert or update on table "bookitem" violates foreign key constraint"bookitem_book_fkey"

在这一点上,任何建议都将不胜感激,因为我已尝试两次解决此问题,但均无济于事。

附:感谢所有帮助我意识到问题是我的代码而不是 heroku 的人!

【问题讨论】:

  • 不确定您正在使用哪种语言/库,但您确定您从该 INSERT...SELECT 中获得了 id 吗?您是否可能试图插入重复的行,以便 not exists 正在查找某些内容而 INSERT 永远不会完成?
  • 你应该用你的语言/框架/库标记问题,让理解它的用户做出回应。
  • 我正在使用 go lang 和数据库 sql。我有一个上面定义的 id var(作为 int64)。我将通过输出它然后手动运行它来检查它。
  • 好的,所以@muistooshort 是对的。我的代码,而不是 heroku 被破坏了。关于如何修复它的任何建议?它本质上是一个返回 id 的 upsert。

标签: sql postgresql go upsert


【解决方案1】:

因此将其更改为 QueryRow 解决了我的问题。我相信它必须与并发问题有关。希望这对未来的人有用。

【讨论】:

    猜你喜欢
    • 2021-12-18
    • 2013-07-08
    • 1970-01-01
    • 2020-09-15
    • 2023-04-05
    • 1970-01-01
    • 2020-07-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多