Relationship associations don't save correctly when not mapped by the PK of either entity

Description

Short vesion: If you have a hasMany relationship that is not mapped by the primary key of either entity, it will fail to save because it will null the key by which the relationship is mapped. Repo link with tests is here: https://github.com/MordantWastrel/quick/tree/hasManyNonPK

Long version:

Environment: Quick 2.2.1 or 2.2.2, QB 6.3.4, MSSQL 2019

We have an edge case in which a relationship on User to a membershipYear table is mapped not by userID or postID, but an external key provided by a third party - in our case, it's 'AYSO ID' (for the organization 'AYSO'), such that:

  • Every User has an AYSOID, but

  • Multiple users may have the same AYSOID (since each individual league may have two "incarnations" of the same member)

  • The member's membership status is saved only once per year and must apply to all users with that member's AYSO ID.

The relationship looks like this:

It works just fine for getting, and for saving as an independent object (getInstance, fill, save, then re-load the user). But if you try and save it through the relationship, either by:

 

It will attempt to INSERT the background check object with no AYSOID, even though one exists on the object.

I've updated the test suite to add a table called externalThings and a relationship from user that is mapped by an externalId field. I've also added a second user with the same externalId as userID 1, though I didn't end up needing it to reproduce the issue; I left it in there and updated all the other tests expecting only three users. If I get some time in the next couple of months, it might be nice to re-write the test suite using schemaBuilder rather than Application.cfc queryExecutes with MySQL syntax, if that's OK with you.

Environment

None
Fixed

Assignee

Eric Peterson

Reporter

Samuel W. Knowlton

Labels

None

Priority

Major