On a per query basis, would it be possible to prefix the column names that get generated in the sql statements with a schema name (for postgres)?
It would be cool to be able to dynamically select which schema you want the call to go to?
example, say your postgres tables look like:
customer1.users customer2.users
When I make a db call, I can choose which schema to use.
Possible?
Comment From: jinzhu
You can initialize two db connections, for example:
db1, err := gorm.Open("postgres", "user=gorm dbname=customer1 sslmode=disable")
db2, err := gorm.Open("postgres", "user=gorm dbname=customer2 sslmode=disable")
db1.First(&user1)
db2.First(&user2)
Comment From: debugging
Thanks but I'm referring to postgres schema's not seperate databases. A database schema is more like a 'namespace' for tables.
So its like:
database1.schema1.users database1.schema2.users
http://www.postgresql.org/docs/9.0/static/ddl-schemas.html
Comment From: jinzhu
Hi @debugging
Sorry for the misunderstanding, gorm could support schema by using Table https://github.com/jinzhu/gorm#specifying-the-table-name
For example:
db.Table("schema1.users").First(&user1)
db.Table("schema2.users").First(&user2)
Comment From: acasajus
That's not super useful. What if you want to use an association. What would be nice is to set the default schema per connection. It's done using
SET search_path TO myschema;
This way all queries onwards will use that schema. Is that somehow possible to set this for all connections?
Comment From: haymps
I'm updating 10K rows using go routines and it seems as if the search_path gets lost. I tried updating the search_path within the routine I was calling with go f(x), but it still was getting lost. I'm switching to the db.Table for now.
`
{
go DeleteDP(db, deliveredPrice, client)
}
func DeleteDP(db *gorm.DB, deliveredPrice DeliveredPrice, client) { db.Exec("SET search_path TO " + client) db.Delete(&deliveredPrice) } `
Comment From: dvaldivia
@haymps did you found a solution around this?
Comment From: starfishs
gorm.Open("postgres", "dbname=YourDB search_path=YourSearchPath")