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

Add a way to override return value of Dialector.DefaultValueOf() #34

Closed
remyoster opened this issue Mar 10, 2021 · 2 comments
Closed

Add a way to override return value of Dialector.DefaultValueOf() #34

remyoster opened this issue Mar 10, 2021 · 2 comments
Assignees

Comments

@remyoster
Copy link

Describe the feature

Add a way to override the return value of Dialector.DefaultValueOf()

Motivation

For our project we use sqlite driver for the tests.
When we insert a default value with gorm:default:null it inserts DEFAULT instead of NULL. The field is a primary key and has AutoIncrement disabled because we are using snowflake.
We couldn't find any solution except by modifying the driver code which is not satisfying

@jinzhu
Copy link
Member

jinzhu commented Nov 30, 2021

can you create a PR on playground, don't get the idea.

btw, please recreate an issue on github.com/go-gorm/gorm with the PR link.

Thank you.

@jinzhu jinzhu closed this as completed Nov 30, 2021
samuelncui pushed a commit to samuelncui/gorm-sqlite that referenced this issue Oct 4, 2023
@schefi
Copy link

schefi commented Jan 8, 2024

I had the same problem. The fact that SQLite does not understand the expression "DEFAULT" is even noted in the source code:

func (dialector Dialector) DefaultValueOf(field *schema.Field) clause.Expression {
	if field.AutoIncrement {
		return clause.Expr{SQL: "NULL"}
	}

	// doesn't work, will raise error
	return clause.Expr{SQL: "DEFAULT"}
} 

I would suggest the addition of a simple

	if field.HasDefaultValue {
		return clause.Expr{SQL: field.DefaultValue}
	}

before returning "DEFAULT". I have done this by reimplementing the Dialector interface in my project, which calls the sqlite.Dialector interface functions except with this small override before calling sqlite.DefaultValueOf()

It would be more efficient if this small change would be included upstream.
Thank you!

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