GORM Playground Link

https://github.com/go-gorm/playground/pull/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

Comment From: xuxing421

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

Comment From: a631807682

Is there any real usage scenario?

Comment From: MohammedTawfik

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

Comment From: southwolf

@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.

Comment From: a631807682

@southwolf Not currently supported

Comment From: southwolf

@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)