Huh I just checked and oddly doing `order = Payment.new` twice issues `UPDATE`, not `INSERT`. I created a small script with some comments here: https://github.com/DawidJanczak/sequel-test-one-to-one/blob/main/sequel_one_to_one.rb There is also a migration inside `migrations` directory.
Please let me know if you need anything else, thanks! On Thursday, 11 February 2021 at 12:44:39 UTC+8 Jeremy Evans wrote: > On Wed, Feb 10, 2021 at 8:37 PM Dawid Janczak <[email protected]> wrote: > >> Hello! >> >> I have a one_to_one relation between an order and transaction >> (`one_to_one :transaction` inside `Order` class). In addition I have a FK >> constraint on `order_id` on `transactions` table. Assigning the transaction >> with `order.transaction = Transaction.new(attrs)` works just fine, but then >> when I (incorrectly) try to add another one I'm getting a >> `NotNullConstraintViolation` on `order_id` column. Is there a reason for >> this and not raising `UniqueConstraintViolation`? >> >> Notice that this only happens if I use `order.transaction = >> Transaction.new(attrs)` setter, if I instead do >> `Transaction..create(attrs.merge(order_id: order.id))` I get the correct >> error raised. >> > > If you submit a INSERT query where the inserted row violates both a NOT > NULL constraint and a UNIQUE constraint/index, which exception is raised is > up to the database. It makes sense that the database would raise the NOT > NULL constraint violation first as that is cheaper to check than a UNIQUE > constraint/index. > > If you don't think that is what is going on, please post a minimal self > contained example showing the problem. > > Thanks, > Jeremy > -- You received this message because you are subscribed to the Google Groups "sequel-talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/sequel-talk/904a847a-f2fc-4040-b2af-dd6ad85f7b5en%40googlegroups.com.
