Associations
Crecto provides helper macros for defining associations between data models.
Has Many and Belongs To
class User < Crecto::Model # schema...
has_many :images, Imageend
class Image < Crecto::Model # schema...
belongs_to :user, Userend
Has Many options
through
- Specifying a join tabledependent: :destroy
- On deletion, will delete all associated records firstdependent: :nullify
- On deletion, will nullify all associated records foreign keysforeign_key
- Specify the foreign key field of of the associated tableforeign_key: :user_id
Has One
class User < Crecto::Model # schema...
has_one :address, Addressend
Has One options
dependent: :destroy
- On deletion, will delete all associated records firstdependent: :nullify
- On deletion, will nullify all associated records foreign keysforeign_key
- Specify the foreign key field of of the associated tableforeign_key: :user_id
Has Many Through / Join tables
class User < Crecto::Model # schema...
has_many :memberships, Membership has_many :groups, through: :membershipsend
class Group < Crecto::Model # schema...
has_many :memberships, Membership has_many :users, through: :membershipsend
class Membership < Crecto::Model belongs_to :user, User belongs_to :group, Groupend
Setting associations
user = Repo.get!(User, 1)image = Image.newimage.user = userRepo.insert(image)
Nil-check associations
If an association is not loaded, the normal accessor will raise an exception
user = Repo.get!(User, 1)user.posts? # niluser.posts # raises Crecto::AssociationNotLoaded
For `has_many` preloads, the result will always be an array
user = Repo.get!(User, 1, Query.preload(:posts))user.posts? # Array(Post)user.posts # Array(Post)
For belongs_to
and has_one
preloads, the result may still be nil if no record exists. If the association is nullable, always use the association_name?
syntax.
post = Repo.get!(Post, 1, Query.preload(:user))post.user? # nilpost.user # raises Crecto::AssociationNotLoaded