Skip to content

feat: create belongs-to-many related entities - #331

Open
elpete wants to merge 2 commits into
nextfrom
feat/84-belongs-to-many-create
Open

feat: create belongs-to-many related entities#331
elpete wants to merge 2 commits into
nextfrom
feat/84-belongs-to-many-create

Conversation

@elpete

@elpete elpete commented Aug 22, 2026

Copy link
Copy Markdown
Collaborator

Closes #84

Review

The two relationship types in this request have different semantics.

  • belongsToMany.create: 9/10. The parent, related entity, and single pivot row are unambiguous, so Quick should support this.
  • Implicit hasManyThrough.create: 3/10. A parent can have several intermediate entities (and an arbitrarily deep chain), so Quick cannot safely infer which intermediate row should own the new terminal entity.
  • Overall recommendation: 7/10, implementing the unambiguous many-to-many case and declining an implicit through-chain write until an API explicitly identifies every required intermediate.

Reasons for

  • Makes belongsToMany.create() match developer expectations.
  • Returns the newly created related entity.
  • Creates the pivot row automatically.
  • Supports non-key pivot attributes without requiring a pivot Quick entity.
  • Reuses attach(), which now also accepts optional pivot attributes.

Reasons against

  • The related insert and pivot insert are separate operations; callers needing atomicity should use their transaction boundary.
  • The same pivot attribute struct is applied to every ID when using multi-ID attach.
  • A generic hasManyThrough.create would be ambiguous and could associate data with the wrong intermediate entity.

Implementation

var tag = post.tags().create(
    { name : "testing" },
    { context : "created through relationship" }
);

The method creates and returns the loaded related entity, then attaches it. Pivot key values are generated by Quick and cannot be overwritten by the additional pivot attributes.

Test-first evidence

Before implementation, the public post.tags().create(...) call fell through the relationship builder and errored with Method does not exist on QueryBuilder [create].

The integration test now verifies the returned Tag, persisted target row, relationship association, and additional pivot-field value.

Validation

  • Focused belongs-to-many spec: 3 passed, 0 failed, 0 errors
  • Full suite: 496 passed, 0 failed, 0 errors, 3 skipped
  • Formatter completed
  • git diff --check passed
  • Tested with qb@14.0.0-beta.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant