GORM Playground Link
https://github.com/go-gorm/playground/pull/268
Description
I added a float64 field named Points to the l User model with this declaration:
Points float64 `gorm:"precision:16;scale:2"`
when I save a floating number like 16.73, the value saved in database is 16. The scale tag in declaration does not work.
Comment From: jinzhu
Sorry for the problem, just fixed that, please use postgres driver v1.0.8 to recreate the schema.
Comment From: inamvar
@jinzhu thanks. I've updated postgres driver from v1.0.5 to v1.08. I'm using multi schemas pattern in database. now I'm getting this error on migration (there was no error in v1.0.5):
gorm.io/driver/postgres@v1.0.8/migrator.go:249 ERROR: more than one row returned by a subquery used as an expression (SQLSTATE 21000); ERROR: more than one row returned by a subquery used as an expression (SQLSTATE 21000)
[5.409ms] [rows:-] SELECT description FROM pg_catalog.pg_description WHERE objsubid = (SELECT ordinal_position FROM information_schema.columns WHERE table_name = 'account_groups' AND column_name = 'name') AND objoid = (SELECT oid FROM pg_catalog.pg_class WHERE relname = 'account_groups' AND relnamespace = (SELECT oid FROM pg_catalog.pg_namespace WHERE nspname = CURRENT_SCHEMA()))
Comment From: jinzhu
the error depends on your data, not GORM related, use IN like:
SELECT description FROM pg_catalog.pg_description WHERE objsubid IN (SELECT ordinal_position FROM information_schema.columns WHERE table_name =...))
Comment From: inamvar
@jinzhu okay. but I'm not using this query. its from gorm migrator. to fetch table catalog information. I can't change that
here the migrator.go from gorm postgres driver that is making that query:
Comment From: inamvar
@jinzhu I'm not sure what is the right way to switch between schemas in runtime. I'm executing set search_path command before any query execution.
Comment From: jinzhu
Hi @inamvar
I'm using multi schemas pattern in database.
Could you post some code for how are you using it like?
Comment From: inamvar
Hi @inamvar
I'm using multi schemas pattern in database.
Could you post some code for how are you using it like? @jinzhu
sure. this is a loop for list of tenants. each tenant has its own schema and all schemas should be migrated. I'm getting that error for second schema migration. for the first schema everything is ok. Here some code I'm using to migrate tables:
for _, tenant := range tenantsList {
service.TenantId = tenant.TenantId
err = InitMigrations(tenant, db)
if err != nil {
continue
} else {
err, _ := service.SeedFromJsonFiles(nil)
if err != nil {
return err
}
}
}
func InitMigrations(tenant models.Tenant, db *gorm.DB) error {
schemaName := fmt.Sprintf("tenant_%d", tenant.TenantId)
row := db.Raw("SELECT schema_name FROM information_schema.schemata WHERE schema_name = ?", schemaName).
Row()
err := row.Scan(&schemaName)
if err != nil {
utils.Log("error", err)
utils.Log("error", fmt.Sprintf("Schema for tenant %d does not exists", tenant.TenantId))
return err
}
session := db.Begin()
session.Exec(fmt.Sprintf("set search_path to tenant_%d", tenant.TenantId))
err = session.AutoMigrate(&models.AccountGroup{})
///
/// ..... auto migrate other tables same as above
///
if err != nil {
utils.Log("error", err.Error())
session.Rollback()
db.Exec("set search_path to public")
return err
} else {
// session.Exec(fmt.Sprintf(queries.ADDTenantIdMemberIdUniqQuery, tables.TenantMembers, tables.TenantMembers))
session.Exec("set search_path to public")
session.Commit()
return nil
}
}
Comment From: premkumar-gg
For anyone else stuck with the above problem, upgrading to a newer version of gorm postgres driver fixed the issue for me.
The faulting query had been fixed to include the schema name: https://github.com/go-gorm/postgres/blob/v1.3.5/migrator.go#L215
https://github.com/go-gorm/postgres/pull/43