Issue
This Content is from Stack Overflow. Question asked by Bozhilov
To further explain my problem I have a database in postgreSQL. With the intent of the database to look cleaner I split most of the tables in between two schema-s one for the “users” and a second for the so called “teams”. As it’s expected I’ve created a many to one relation between the teams.team table and the users.user table by giving the teamID inside the user table. I’m writing the backend for the current software in GoLang and decided to use Gorm as way to handle the database. I’ve gotten somewhat the hang of gorm and figured out how to use schema-s and to test that Gorm can handle my database solution I decided with the structures, I’ve created to represent the tables, to create again all my tables in the db.
For the different schema-s I’ve had to make different connections and specify that I want them for the specific schema and since I have to write that there’s a connection to Team inside User it thinks it’s in the same schema
type User struct {
gorm.Model
FirstName string
LastName string
Email string
ElsysEmail string
Mobile string
Password string
InfoID uint
Info Info
SecurityID uint
Security Security
RoleID uint
Role Role
TeamID uint
Team Team
LastLogin time.Time
}
For anyone having a problem with creating tables inside a schema here’s my solution:
dsn := "host=localhost user=postgres password=password dbname=ht9 port=5432 sslmode=disable"
db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{NamingStrategy{TablePrefix: "schemaName."}})
if err != nil {
log.Fatal(err)
}
disclaimer : my problem is almost identical to this one – GORM model foreign key to different Postgres schema – but no one’s answered it yet
Solution
This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.
This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.