ORM-19: Implement HasMany and HasOne #19
Labels
No labels
area:core
area:database
area:migrations
area:model
area:relations
area:schema
area:tests
breaking-change
size:M
size:S
tracking
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
NoctisWolfshadow/tessera-orm#19
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Depends on: #18
Context
The foreign key lives on the child table (
posts.user_id). A relation is declared once as a typedconstdescriptor, likeColumn, and an instance method is sugar over it..of(&parent)returns a small handle holding a copy of the parent's key (it borrows nothing), which behaves like aQuery<Child>pre-filtered by the parent.Tasks
HasMany::<P, C>::new(fk: Column<C, i64>, set_fk: fn(&mut C, i64)), const-constructible.HasOnetakes the same arguments..of(&parent)returnsHasManyOf<C>/HasOneOf<C>. The handle exposesquery() -> Query<C>(C::query().filter(fk.equals(parent_id))) and delegatesfilter,order_by,limit,offset,allandfirst.HasManyhandle:save(&mut child)sets the FK throughset_fkand callschild.save();create(child) -> Result<C>does the same for a new child.user.posts()anduser.profile().Error::NotPersisted.Example
Acceptance criteria
firston the handles work.saveandcreateset the FK on the child.NotPersisted.Notes
If a closure in a
constinitialiser causes trouble on your toolchain, a namedfn set_user_id(p: &mut Post, id: i64)is equivalent.BelongsToMany(read side) #21load_for) to avoid N+1 queries #23