Postgres可重复读取 - 如何删除现有行?

时间:2014-05-22 12:06:02

标签: sql postgresql transactions

首先,我使用ORM生成查询。 我的模型看起来像这样:

Category hasMany CategoryTranslation
CategoryTranslation.slug is UNIQUE

我想要的是更新涉及3个步骤的类别:

  1. 更新类别属性
  2. 删除属于此类别的所有CategoryTranslation
  3. 创建新的CategoryTranslations
  4. 这是SQL生成的:

    Executing (777e5f34-1078-4b1b-9eb6-09e117bbf751): START TRANSACTION;
    Executing (777e5f34-1078-4b1b-9eb6-09e117bbf751): SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;
    Executing (777e5f34-1078-4b1b-9eb6-09e117bbf751): SET autocommit = 1;
    Executing (777e5f34-1078-4b1b-9eb6-09e117bbf751): UPDATE "Categories" SET "id"=20 WHERE "id"=20 RETURNING *
    Executing (777e5f34-1078-4b1b-9eb6-09e117bbf751): DELETE FROM "CategoryTranslations" WHERE "id" IN (SELECT "id" FROM "CategoryTranslations" WHERE "CategoryId"=20)
    Executing (777e5f34-1078-4b1b-9eb6-09e117bbf751): INSERT INTO "CategoryTranslations" ("name","slug","description","seoTitle","seoDescription","language","CategoryId","id","createdAt","updatedAt") VALUES ('123f','sdcfsdf','sdf','sdf','sdf','en-us',NULL,DEFAULT,'2014-05-22 11:35:18.635 +00:00','2014-05-22 11:35:18.635 +00:00') RETURNING *;
    Executing (777e5f34-1078-4b1b-9eb6-09e117bbf751): ROLLBACK;
    

    问题是在此查询中没有删除CatgoryTranslations,并且插入新的会违反了slug列上的UNIQUE键。 是否可以删除事务中的现有(事务前存在的)行(可重复读取)?或者我应该使用其他事务隔离级别?

    我的目标是完成我在“全有或全无”模式中提到的3个步骤......

0 个答案:

没有答案