GORM Playground Link
https://github.com/go-gorm/playground/pull/185
Description
Using the Postgres driver/database:
I perform a large ETL type job where I receive a DB from a vendor, load it into an isolated schema where a specific user has permissions to create and drop tables. Having imported all the data I run a number of INSERT ... SELECT ...
type statements to massage the original data into my actual database (in the public schema of the same database instance).
To do this we use AutoMigrate to create the tables in the isolated schema. However in attempting to upgrade GORM to v2 this functionality has broken because the AutoMigrate method appears to drop the schema name provided via TableName
. Other queries appear to preserve the schema name.
I expect AutoMigrate to use TableName in the same manner everything else does and preserve the schema name when provided.
Comment From: jinzhu
Sorry, but the .
won't work when using as part of table name, it has specially meaning in mysql/postgres
Comment From: endophage
I think you may be misunderstanding the problem. I'm using the .
per its syntax in PostgreSQL. I want to query the table "other_schema_table" in the non-default schema "otherschema". In PostgreSQL one would do that by querying SELECT * FROM otherschema.other_schema_table ...
Much of GORM correctly handles these dotted tablenames as expected and creates queries in the form of SELECT * FROM "otherschema"."other_schema_table" ...
However AutoMigrate does not operate consistently with the rest of GORM. Either it should operate consitently, or you need to document how I use AutoMigrate to creates tables in a non-default schema and apply that consistently to all the other queries.
Comment From: jinzhu
Fixed, thank you for your report, need to upgrade driver & gorm to latest master.
Comment From: endophage
Thank you so much for fixing this so quickly!
Comment From: dpinart-uxland
Hi I'm quite noob in Go and also in postgres. I'm tryign to Automigrate my models in an isolated schema but the tables are still being created in the 'public' db schema. I can't see this fix
In my go.mod files I can see I'm importing gorm.io/driver/postgres v1.0.5 gorm.io/gorm v1.20.5 Am I doing something wrong?
Comment From: Zero-Fanker
I'm afraid this issue still exists, if it works as a foreign key struct:
Resusing the example provided by endopage:
type OtherSchemaTable struct {
AColumn string `gorm:"primary_key"`
LinkedID int
Linked OtherSchemaTableLinked `gorm:"foreignKey:LinkedID"`
}
func (OtherSchemaTable) TableName() string {
return "otherschema.other_schema_table"
}
type OtherSchemaTableLinked struct {
ID int
Foo string
}
func (OtherSchemaTableLinked) TableName() string {
return "schemaelsewhere.other_schema_table_linked"
}
func TestGORM(t *testing.T) {
if err := DB.AutoMigrate(&OtherSchemaTable{}); err != nil {
t.Errorf("Failed to automigrate: %s", err)
}
result := make([]OtherSchemaTable, 0)
if err := DB.Find(&result).Error; err != nil {
t.Errorf("Failed, got error: %v", err)
}
}