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

Columns with default values are appended onto queries no matter what. #490

Closed
OliwerXi opened this issue Mar 20, 2022 · 4 comments
Closed

Comments

@OliwerXi
Copy link

Are columns that have default values yet no value assigned to said field supposed to be appended onto a query?
For example - take this table;

type User struct {
  ID    string `bun:"id,notnull,type:uuid,default:gen_random_uuid()"`
  Email string `bun:",notnull"`
}

If I were to try and insert this accordingly, without including ID for said instance, for example
db.NewInsert().Model(&User{ Email: "email" }).Exec(context) - it would cause the 22P02 (invalid text representation) error due to id not being specified and emptied out on query appendices.

Is there a way of handling this manually or ignoring fields on specifically INSERT operations?

@imraan-go
Copy link

Yes, You can use bun:",nullzero" tag to use DEFAULT in sql insert. The default tag is used for DDL only. Although I find it odd, @vmihailenco is there any way to improve it? There should be a better way to tell bun to ignore certain fields when inserting.

@OliwerXi
Copy link
Author

OliwerXi commented Mar 20, 2022

Yes, You can use bun:",nullzero" tag to use DEFAULT in sql insert. The default tag is used for DDL only. Although I find it odd, @vmihailenco is there any way to improve it? There should be a better way to tell bun to ignore certain fields when inserting.

I see. That most definitely clears up my confusion, thanks. I probably should have looked a little more deeper into the documentation and have found nullzero in the Defining models section, silly mistake. I thought it was odd as well due to the fact that I assumed it automatically handled the field the way nullzero does, but with only default present in the tags since it more or less indicates that the field can be NULL or DEFAULT and to be ignored. But then, I guess there are cases where one would like to perhaps pass said DEFAULT value of a type purposely WITHOUT them being ignored. It makes sense, but if anyone ever comes up with a possibly better way, I think we're all for it. I will proceed with not closing this just in-case vmihailenco decides to fleek in on the matter.

A little bit later: I just read through all tags and I guess scanonly (it although includes updating which some people might not want ig) does what we were on about.

@imraan-go
Copy link

No scanonly doesnt do what you are looking for. Take a look at #489

@vmihailenco
Copy link
Member

I will proceed with not closing this just in-case vmihailenco decides to fleek in on the matter.

I don't have better ideas how to handle this. Sometimes people want to marshal empty values as NULL and sometimes they don't. Explicit nullzero option allows to do both.

No scanonly doesnt do what you are looking for.

I will slightly re-word docs for scanonly option.

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

No branches or pull requests

3 participants