简体   繁体   English

Upsert Postgresql返回ID

[英]Upsert postgresql returning id

Initially I thought this was a heroku issue as, for some bizarre reason this code runs fine locally, but on further investigation I realized id was returning 0 all the time. 最初,我认为这是一个heroku问题,因为出于某种奇怪的原因,该代码在本地运行良好,但是在进一步调查中,我意识到id一直在返回0。 Essentially I am trying to code an upsert that returns the id. 本质上,我正在尝试编写返回id的upsert。 I am working in go with the sql library. 我正在使用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);

Here is the code that triggers the error: 这是触发错误的代码:

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)

The error I get when I run it deployed to heroku is: 当我将其部署到heroku上运行时收到的错误是:

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

At this point any suggestions would be appreciated as I have tried twice to fix this to no avail. 在这一点上,任何建议将不胜感激,因为我已经尝试过两次以解决此问题。

PS Thank you to all of you who helped me realize the issue was my code and not heroku! PS感谢所有帮助我认识到问题是我的代码而不是heroku的人!

So changing it to QueryRow solved my issues. 因此,将其更改为QueryRow解决了我的问题。 I believe it had to due with a concurrency issue. 我认为它必须要考虑并发问题。 Hopefully this is useful to someone down the road. 希望这对以后的人很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM