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

Error when Create New Database Record #6615

Open
MohammedTawfik opened this issue Sep 26, 2023 · 6 comments
Open

Error when Create New Database Record #6615

MohammedTawfik opened this issue Sep 26, 2023 · 6 comments
Assignees
Labels
type:feature_request feature request type:with reproduction steps with reproduction steps

Comments

@MohammedTawfik
Copy link

GORM Playground Link

go-gorm/playground#650

Description

i got the following error "failed to parse nextval(' as default value for int, got error: strconv.ParseInt: parsing "nextval('": invalid syntax
0 failed to parse nextval(' as default value for int, got error: strconv.ParseInt: parsing "nextval('": invalid syntax" when try to create new record in the database i used GORM gen to generate the models and use them when create the database record

@xuxing421
Copy link

xuxing421 commented Oct 8, 2023

It seems like there is something wrong about type changing,trying to check if struct column type is what you expected

@a631807682
Copy link
Member

Is there any real usage scenario?

@MohammedTawfik
Copy link
Author

i created postgres database using migration scripts then use GORM to create the structs then when try to insert any record in the database this error is occured

@southwolf
Copy link

@a631807682 @MohammedTawfik

I'm wondering if the default tag works

I also hit a similar issue. I'm trying to use it in PostgreSQL on an existing table when migrating a Java app to Go, the default value of ID is not set in the table, but it's controlled in Hibernate instead

@SequenceGenerator(name="my_generator", sequenceName = "my_seq")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "my_generator")

But when I try to create a token

type Token struct {
	TokenId            int64 `gorm:"column:token_id;default:nextval('my_seq');primaryKey"`
}

It does not generate TokenId automatically, but reports ERROR: null value in column "token_id" of relation "token" violates not-null constraint.

@a631807682
Copy link
Member

@southwolf Not currently supported

@a631807682 a631807682 added the type:feature_request feature request label Jan 12, 2024
@southwolf
Copy link

@southwolf Not currently supported

Thanks for confirming. Now I am using a workaround

db.Raw("SELECT nextval('my_seq')").Scan(&entity.id)
db.Create(&entity)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:feature_request feature request type:with reproduction steps with reproduction steps
Projects
None yet
Development

No branches or pull requests

5 participants