Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upsert fails with sql.ErrNoRows #84

Closed
oystedal opened this issue Jan 5, 2017 · 2 comments
Closed

Upsert fails with sql.ErrNoRows #84

oystedal opened this issue Jan 5, 2017 · 2 comments
Labels

Comments

@oystedal
Copy link

oystedal commented Jan 5, 2017

Upsert fails, returning sql.ErrNoRows, because Postgres does not return any values when DO NOTHING is used as the conflict action. This happens when there is a conflict on the tuple to be inserted, such as when trying to insert a duplicate primary key/unique.

The expected result would be that Upsert returns no error and updates the object on which Upsert was called.

Schema:

CREATE TABLE video
(
    id serial PRIMARY KEY,
    youtube_id varchar(64) NOT NULL,
    title text NOT NULL
);

ALTER TABLE video ADD CONSTRAINT youtube_id_uniq UNIQUE (youtube_id);

Call:

// A record with YoutubeID="foo" already exists in the database
video := models.Video{YoutubeID: "foo", Title: "abc"} 
if err = video.UpsertG(false, []string{"youtube_id"}, nil); err != nil {
	return nil, err
}

SQL query + parameters:

INSERT INTO "video" ("youtube_id", "title") VALUES ($1,$2) ON CONFLICT DO NOTHING RETURNING "id"
[foo abc]
@nullbio nullbio added the bug label Jan 6, 2017
@aarondl
Copy link
Member

aarondl commented Jan 6, 2017

Please test dev branch to see if this is resolved.

@oystedal
Copy link
Author

oystedal commented Jan 7, 2017

Looks good to me, using the updated code in the dev branch worked.

@aarondl aarondl closed this as completed in 22f7a45 Jan 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants