Model foreign key reference with no id broken
wschmrdrNS opened this issue · comments
Create a model object with a hasMany relationship that has no ID (i.e. it hasn't been saved yet).
Example:
Model bar { @configure('Bar', 'id', 'foo_id', 'whee_id') }
Model foo { @hasmany 'bars' }
Model whee { @hasmany 'bars' }
The associated 'bar' collection has many 'bar' Model objects in it, all with a FK reference to 'whee'.
Attempt to do a check on @foo.bars().all(). It will return all of the 'whee' bar instances because @foo.id is undefined and bar.foo_id is also undefined. Ideally, a record with no ID should return nothing for @ALL() on an associated collection.
This appears to be more of an issue with the associated
function instead of all
and effects also select
and findAllByAttribute
, etc.
A possible fix:
associated: (record) ->
record[@fkey] and (record[@fkey] is (@record.id or @record.cid))
First step will be to see if this is an issue in spine core or just the relation module