You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add the ability to set a foreign key on a model to create relationships #296
It would be great if developers could be a given the option to create one-to-one relationships using foreign keys. This pattern should feel natural for developers who are familiar with relational databases.
Alternatively, the library could implicitly create a userId field on a blogPost if blogPost was declared with a oneOf("user") relationship. I think this is what mirage does.
Tell me, would the usage of transient + derivative properties work for you?
constdb=factory({user: {id: id(Number),// Create a transient "user.city" property.// It can be given initial value but it will never// be set on the user entities.city: transient(oneOf('city')),// Derive a public "user.cityId" foreign key// from the transient "user.city" one-of relationship.cityId: derivative(({ city })=>city.name),},city: {name: id(String),},})constuser=db.user.create({id: 1,city: db.city.create({name: 'London'}),})user.cityId// "London"user.city// undefined
This is a non-existing API (yet).
I'm tackling this precise use case in the Data rewrite and I could use your feedback.
Why not use foreignKey()?
The API you proposed implies that the key given as the value to the user_id property would be identifiable as foreign. But in the context of operations (create/update/etc), those key are taken from the existing extraneous models (like user), and will know nothing about post.userId having to be foreign.
To put plainly, it becomes problematic to ensure the contract with this kind of API.
Activity
kettanaito commentedon Dec 1, 2023
Hi, @canrozanes. Thanks for raising this.
Tell me, would the usage of transient + derivative properties work for you?
I'm tackling this precise use case in the Data rewrite and I could use your feedback.
Why not use
foreignKey()?The API you proposed implies that the key given as the value to the
user_idproperty would be identifiable as foreign. But in the context of operations (create/update/etc), those key are taken from the existing extraneous models (likeuser), and will know nothing aboutpost.userIdhaving to be foreign.To put plainly, it becomes problematic to ensure the contract with this kind of API.