Reputation: 657
I have 2 models with many-to-many relationship like the next:
(declare organisations)
(defentity users
(many-to-many organisations :users_organisations
{:lfk :user_id, :rfk :organisation_id}))
(defentity organisations
(many-to-many users :users_organisations
{:lfk :organisation_id, :rfk :user_id}))
(defentity users-organisations
(table :users_organisations))
I want to insert rows into both models and make a relationship record in the same transaction
(transaction
(let [usr (insert users (values {:email "[email protected]"}))
org (insert organisations (values {:title "My Co."}))]
(insert users-organisation
(values {:user_id (:id usr)
:organisation_id (:id org)}))))
Unfortunately, it doesn't work this way. None of insertions into users or organisations work. Is it possible to do something like that in Korma?
What I try to achieve is something like the next Postgresql statement:
WITH new_user AS (
INSERT INTO users (email)
VALUES ('[email protected]')
RETURNING id
), new_org AS (
INSERT INTO organisations (title)
VALUES ('My Co.')
RETURNING id
)
INSERT INTO users_organisations (user_id, organisation_id)
VALUES ((SELECT id FROM new_user), (SELECT id FROM new_org))
RETURNING *;
Thanks in advance for any help!
Upvotes: 4
Views: 290