Hacker News new | past | comments | ask | show | jobs | submit | bigpigeon's comments login

toyorm v0.3.1-alpha add join query


now toyorm support join operation


yes, codegen have better performance than reflect

but I think text/template to generate code is not really good

it hard to read and error-prone

it is more like a powerful c-macro

I think use go/ast to implement c++ likes template is other way


Whatever works! I just wrote the functions I wanted normally, tested them, then converted them to templates.


when you want to make complex sql,toyorm maybe word better

find the name = "tom" user and it subquery blog title = "first blog" e.g

  brick := toy.Model(&User{}).Where("=",Offsetof(User{}.Name),"tom").
			Preload(Offsetof(User{}.Blog)).Where("=", Offsetof(Blog{}.Title), "first blog").Enter()

  brick.Find(&user)
  // raw sql
  // select id,name,data, from user where name = "tom" limit 1
  // select id,title,content,user_id from blog where user_id = @user.id and title = "first blog"
toyorm select field with Offsetof, it better when you want to refactor struct field name and you can operation main query as sub query


you means use join to query and output data as table? otherwise I think “eager fetching” is better way


Lets say I have a relationship where a User has one Profile.

    type User Struct { Id int ProfileId int Profile Profile }
    type Profile Struct { Id int }
For a load like this:

    var u User
    db.Model(&User{}).Preload("Profile").Where("id = ?", 7).Take(&u)
Gorm will do something like this:

    SELECT * FROM users WHERE id=7;
    SELECT * FROM profiles WHERE id=7;
Where a join would be more desirable (only involves 1 trip to the db):

    SELECT * FROM users JOIN profiles ON profiles.id=users.profileid WHERE users.id=7
This also has a huge effect on when your initial queries aren't restricted by ID. Gorm will do huge "id IN (1,2,3,4)" queries - or sometimes if I need to filter on a relation (i.e. get users where profile.foo = bar), I end up doing the join getting that data anyways, then the eager queries loads that data again.


In "OneToOne" and "BelongTo" Join is match but in "ManyToMany" and "OneToMany" will generate duplicate data

I think add Join method and only support "One Data" preload is good way


toyorm support half hand-writing you can bind the model and use its field in sql e.g https://github.com/bigpigeon/toyorm/blob/master/examples/sim...


preload: data association exec/query template exec: custom exec/query this is my orm great features


Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: