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

Issue while trying to create records and remove the RETURNING statement. #30

Closed
dantepippi opened this issue Jan 21, 2021 · 1 comment
Closed
Assignees

Comments

@dantepippi
Copy link

Your Question

I'm using go-gorm with a Postgres 11 DB and facing an issue where I need to remove the RETURNING clause entirely when creating records. I just want to insert records and get nothing back. In go-pg I could use Returning("null") but I couldn't find a way to do it with GORM.

I have many relations on the database that doesn't support RETURNING statements, so when I try to insert records I get the error "ERROR: cannot perform INSERT RETURNING on relation X".

I tried creating the db connection with the parameter WithoutReturning: true. But then I get a different error "LastInsertId is not supported by this driver". It still tries to get the last inserted id anyway.

Thank you very much for your time!

The document you expected this should be explained

There should be a mention to the RETURNING statement on the "Create" section of the documentation and instructions on how to get rid of it if my query won't support.

https://gorm.io/docs/create.html

Expected answer

A polite answer on how to create records when my DB is not supporting RETURNING statements would be great! =D

@dantepippi
Copy link
Author

Copying my Stackoferflow question, where I wrote with a simple code to demo the problem:

I'm using go-gorm with a postgres 11 DB and facing an issue where I need to remove the RETURNING clause entirely when creating records (that statement seems to be included by default). I just want to insert records and get nothing back, except for errors.
I have some complex relations on the database that won't support RETURNING statements, so when I try to insert like this: (made code simpler for brevity)

type Cargo struct {
	Id   int64 `gorm:"primaryKey"`
	Name string
}
dsnString := fmt.Sprintf("host=%s ...") 
db, _ := gorm.Open(postgres.New(postgres.Config{DSN: dsnString}), &gorm.Config{})

cargo := Cargo{Name: "test"}
db.Create(cargo)

I get the error "ERROR: cannot perform INSERT RETURNING on relation X".

I tried creating the db connection with the parameter WithoutReturning: true:

db, _ := gorm.Open(postgres.New(postgres.Config{DSN: dsnString, WithoutReturning: true}), &gorm.Config{})

But then when I try db.Create(cargo) I get a different error: "LastInsertId is not supported by this driver". It seems to be still trying to get the last inserted id anyway.

In go-pg I could use db.Model(x).Returning("null").Insert(cargo) but I couldn't find a way to do it with go-gorm. Any help is greatly appreciated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants