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

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -