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

AutoMigrate failed when type has custom size or precision #121

Open
ghost opened this issue Nov 1, 2022 · 2 comments
Open

AutoMigrate failed when type has custom size or precision #121

ghost opened this issue Nov 1, 2022 · 2 comments
Assignees

Comments

@ghost
Copy link

ghost commented Nov 1, 2022

GORM Playground Link

type Product struct {
    Price float64 `gorm:"type:decimal(10,2);not null"`
}

...

DB.AutoMigrate(Product{})

Description

The database is initialized normally for the first time, but it crashes after restarting the application, returning the error invaild DDL, unbalanced brackets

According to my repeated debugging, the problem appears in migrate.go
At line 89, it would replace decimal (10,2) to ?, 2)

@ghost ghost assigned jinzhu Nov 1, 2022
@ghost ghost changed the title AutoMigrate failed when type has custom size,precious AutoMigrate failed when type has custom size or precision Nov 1, 2022
@astockwell
Copy link

I'm having the same issue, datatypes that would normally work with sqlite (they're targeted at postgres, things like gorm:"type:varchar(100)" and gorm:"type:numeric(3,1)") now do not.

@astockwell
Copy link

astockwell commented Feb 17, 2023

To add more clarity (on the way to a reproducible, failing test)

  • This actually works fine gorm:"type:varchar(100)"
  • This fails with error "invaild DDL, unbalanced brackets": gorm:"type:numeric(3,1)"

This occurs whether using package gorm.io/driver/sqlite or github.com/glebarez/sqlite.

Interestingly, if you change db.AutoMigrate(&MyStruct{}) to db.Migrator().CreateTable(&MyStruct{}), it works (when creating new tables/a new database). But AutoMigrate() fails even when creating new tables/a new database.

UPDATE: It appears that a struct field with gorm:"type:numeric(3,1)" does work as expected in normal circumstances, but I get the above "unbalanced brackets" error when the table/struct also has a many2many relationship with another table via a ternary/join table.

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

2 participants