orm - Gorm Golang fetching a collection and its relationships -
i started using golang , decided give gorm try orm. works pretty on things, orms it's limited. luckily ties database/sql can custom queries easily.
i'm wondering if there other way in gorm: have struct companies, companies have 1 many relationships w/ emails, addresses, , phones. use following code in gorm pull list of companies , corresponding info. use gorm's preload function.
db.dbaccess. model(&companies). count(&dbinfo.count). order("companies.id asc"). offset(offset). limit(length). preload("addresses"). preload("phones"). preload("emails"). find(&companies)
this works fine. feel there way accomplish without preload function. ideas?
you can load related entities later (only when/if needed), using db.related shown in the documentation :
// user has many emails db.model(&user).related(&emails) //// select * emails user_id = 111; // user_id foreign key, 111 user's primary key's value // specify foreign key db.model(&user).related(&emails, "profileid") //// select * emails profile_id = 111; // profile_id foreign key, 111 user's primary key's value
i not see way in documentation.
Comments
Post a Comment