how to use serial within compound primary key
I want the order to go from 0 to n depending on the references shared with purchase_id
21 Replies
Could you explain what you mean a little more? I'm not sure I quite get ya
order is the field that determines which order the refund should be applied.
each refund attaches to a purchase
so its a many to one relation
I want to have the order field default to its insertion order
something like
i don't know exactly how to do the sql
I'm still uhhhh, not exactly with you on this one.
Do you mean that the
order
field should be the count of purchases
that have the purchase_id
that correlates to this refund?it should be the count of refunds linking to the purchase
refunds are many purchase is one
Well, you definitely wouldn't put that as part of the refund table
That seems like a bad idea from the get-go
What you should probably do is get the purchase by the id and have a field there that's the magic sql operator thing, counting the refunds
I just want to have an order to apply the refunds in, I can obviously count it manually. But I thought about whether there was a way to do it automatically on sql-insert.
ATM I'm not using it much but I just want to be sure that I'm certain about the order in which the refunds should be applied.
I'm honestly not sure.
There's probably a way to do it, but there's no way that that's a "good idea" or optimal.
However. Since the refund id is serial you can just order them by that and then the applying order is completely deterministic.
Or by the timestamp. I didn't notice you had a timestamp field.
I'll program some more and find out. I don't expect this to be a problem. Maybe I'm overengineering a bit
Also, highly recommend setting up the relations as """proper""" relations i.e.
That way you can more easily do relational queries later
I have the soft relations all set up already ^^
Ah ok that's good
export const purchase_relations = relations(purchase, ({ many }) => ({
items: many(purchase_item),
refunds: many(refund),
}))
export const refund_relations = relations(refund, ({ one, many }) => ({
purchase: one(purchase, {
fields: [refund.purchase_id],
references: [purchase.id],
}),
items: many(refund_item),
}))
message sender?
In that case, you don't really need
can just do
if you only intend on using the db with this code, that's just code that makes the db enforce that rule and since your code also enforces that anyways it seems kinda pointless
I want an "null" id
Whoops, I copied some code instead of writing the relation from scratch (I'm working on something on the main monitor) and I forgot to delete that part, my bad