我有 2 个具有多对多关系的模型,如下所示:
(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))
我想在两个模型中插入行并在同一个事务中创建关系记录
(transaction
(let [usr (insert users (values {:email "john@mail.me"}))
org (insert organisations (values {:title "My Co."}))]
(insert users-organisation
(values {:user_id (:id usr)
:organisation_id (:id org)}))))
不幸的是,它不能以这种方式工作。对用户或组织的任何插入都不起作用。是否有可能在 Korma 做类似的事情?
我试图实现的是类似于下一个 Postgresql 语句:
WITH new_user AS (
INSERT INTO users (email)
VALUES ('john@mail.me')
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 *;
提前感谢您的帮助!