Using Prisma with Postgres. Have a question that i...
# orm-help
d
Using Prisma with Postgres. Have a question that is more modeling/DB related. I have a PaymentMethod model. A User can have multiple PaymentMethods. Only 1 can be default. On my PaymentMethod model, I have a
default: boolean
. The model also has a
userId: number
since it belongsTo a User. For any userId on the PaymentMethod, only 1 PaymentMethod can be
true
. Any simple way to manage this? Right now in my API calls, if a user wants to make a PaymentMethod default, I just query for that PaymentMethod and the current default PaymentMethod and set one to true and the other to false. It is simple, basic and works, but curious to explore other options that can make this type of situation even simpler for me.
🙌 1
j
I guess you could have a normal one-to-many relationship with the user and the PaymentMethods and then have an additional field called “defaultPayment” on the User object that is a one-to-one relationship to the PaymentMethod? I.e. it stores that foreign key?
👍 3
a
You can do it with two queries first one update many where default is true make it false Second update where payment id and make default true
d
@Jan Wilhelm I like that approach. It probably also doesn't make sense for a Payment Method to know it is the default compared to other Payment Methods... if that makes sense. I think I'll go that route.
👍 1