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

Postgres: AutoMigrate not stable for type:varchar;default:'' #6543

Closed
moolitayer opened this issue Aug 23, 2023 · 3 comments
Closed

Postgres: AutoMigrate not stable for type:varchar;default:'' #6543

moolitayer opened this issue Aug 23, 2023 · 3 comments
Assignees
Labels
type:with reproduction steps with reproduction steps

Comments

@moolitayer
Copy link

moolitayer commented Aug 23, 2023

GORM Playground Link

go-gorm/playground#644

Description

Every time we run AutoMigrage we get the same

ALTER TABLE "X"."Y" ALTER COLUMN "Z" SET DEFAULT '';;

Even thought the column definition is already correct. We currently cannot ignore this as we generate migration files for every PR that changes the schema. Also no known workaround.

@github-actions github-actions bot added type:missing reproduction steps missing reproduction steps type:with reproduction steps with reproduction steps and removed type:missing reproduction steps missing reproduction steps labels Aug 23, 2023
@moolitayer
Copy link
Author

@jinzhu PTAL at this issue with a reproduction. Also, not sure if that's the issue, but on debugging I noticed in (migrator/migrator.go)[https://github.com/go-gorm/gorm/blob/master/migrator/migrator.go#L439C25-L439C25] that the default from the DB is read as ''::TEXT

@moolitayer moolitayer changed the title Postgres: Automigrate Postgres: AutoMigrate not stable for type:varchar;default:'' Aug 23, 2023
@moolitayer
Copy link
Author

Thanks @jinzhu! much appreciated.

@artlo
Copy link

artlo commented Aug 28, 2023

Doesn't it loose the default value?

I've also found this issue but in case of model generation. If the text field has the empty string as default value it generates model with following tag: default:"''::text", so the table column will have the value ::text.
I tried to fix it with the following line:
column.DefaultValueValue.String = regexp.MustCompile('?(.*\b|)'?:+[\w\s]+$).ReplaceAllString(column.DefaultValueValue.String, "$1")

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

No branches or pull requests

3 participants