martenframework / marten

The pragmatic web framework.
https://martenframework.com
MIT License
405 stars 23 forks source link

Make it possible to pre-select one-to-one reverse relations with query sets #119

Closed ellmetha closed 8 months ago

ellmetha commented 1 year ago

Description

Presently, it is only possible to pre-select direct relations (such as records targeted by many-to-one or one-to-one fields) through the use of #join when defining query sets. While reverse relations defined in the context of many-to-one fields could be prefetched through the use of the upcoming #prefetch helper (#104), pre-selecting records targeted by one-to-one reverse relations is something that should be possible through the use of the existing #join helper.

For example, given the following definition:

class User < Marten::Model
  field :id, :big_int, primary_key: true, auto: true
  field :username, :string, max_size: 255
end

class Profile < Marten::Model
  field :id, :big_int, primary_key: true, auto: true
  field :user, :one_to_one, to: User, related: :profile
end

It should be possible to retrieve all the users and their associated profiles in one query with:

User.all.join(:profile)