When having a one to one field, is there a way I c...
# orm-help
s
When having a one to one field, is there a way I can ensure that both models have the same primary key or id.
n
Hey shahrukh 👋 Can you please elaborate a bit? Maybe if you can share the schema and let us know what are you trying to achieve?
s
Hi Nurul, For example, say I have created a User model and there's a User Setting Model that is created along with it. I want both to have the same ids. As you can see in Django's ORM here, you can set the parent object as a primary key. https://docs.djangoproject.com/en/4.0/topics/db/examples/one_to_one/ Makes references data easier. But, I think I can use the cascade feature to ensure the same. I read the docs on it, will test it today.
n
Ah I see, I think you should be able to achieve that by using something like uuid You can generate uuid in advance like this
Copy code
import { v4 as uuidv4 } from 'uuid';
uuidv4(); // ⇨ '9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d'
And while creating the User record as well as the User Setting record you can pass this same id in both the create queries.