我正在try 执行一个迁移,将"Questions"表分成两个表:"Campaign"和"Campaign_Actions",其中Campaign包含问题标题,Campaign操作通过Quest_id列引用问题id,然后通过Campaign_id列引用新创建的Campaign.

我try 使用关键字asinto返回初始SELECT中的字段.我看到的所有示例都是使用值插入到多个表中.我在将新创建的活动ID与插入到"Campaign_Actions"表中的现有问题ID相关联时遇到了问题.我试过几种方法,但这是我最新的try :

    WITH campaign_questions AS (
      INSERT INTO "campaigns" (name, tenant_id)
      SELECT 
        q.title as name,
        q.tenant_id as tenant_id
      FROM "questions" as q
      RETURNING 
        id into campaign_id,
        q.id into question_id, 
        q.app_area_id into app_area_id
    )
    INSERT INTO "campaign_actions" (campaign_id, question_id, app_area_id) 
    SELECT campaign_id, question_id, app_area_id,
    FROM campaign_questions

我还考虑过做一些其他的事情:在活动上添加一个临时列来保存问题ID和应用程序区域ID,这样我就可以从插入操作中返回它们,然后使用它们填充Campaign_Actions表,然后删除这些列.

以下是问题、活动和活动操作表的DBML:

table questions {
  id uuid [pk, unique, default: `uuid_generate_v4()`]

  app_area_id uuid [ref: > app_areas.id]

  tenant_id text [not null]

  title text [not null]
  subtitle text

  type text [not null]

  data jsonb

  // Timestamps
  created_at timestamp [not null, default: `now()`]
  updated_at timestamp [not null, default: `now()`]
  deleted_at timestamp

  indexes {
    tenant_id [type: btree]
  }
}

table campaigns {
  id uuid [pk, unique, default: `uuid_generate_v4()`]
  
  tenant_id text [not null]

  name text [not null]
  description text
  
  start_at timestamp
  end_at timestamp
  paused_at timestamp
  closed_at timestamp

  published_at timestamp

  // Timestamps
  created_at timestamp [not null, default: `now()`]
  updated_at timestamp [not null, default: `now()`]
  deleted_at timestamp

  indexes {
    tenant_id [type: btree]
  }
}

table campaign_actions {
  id uuid [pk, unique, default: `uuid_generate_v4()`]
  
  tenant_id text [not null]
  campaign_id uuid [not null, ref: > campaigns.id]

  type text [not null, default: "question"]
  question_id uuid [ref: > questions.id]
  display_type text

  app_area_id uuid [ref: > app_areas.id]
  
  // Timestamps
  created_at timestamp [not null, default: `now()`]
  updated_at timestamp [not null, default: `now()`]
  deleted_at timestamp

  indexes {
    tenant_id [type: btree]
    campaign_id [type: btree]
    question_id [type: btree]
  }
}

我想有更好的办法或者我错过了什么.如有任何帮助,我们不胜感激!

推荐答案

以下查询通过在执行插入之前生成活动ID来关联活动ID和问题ID:

WITH id_pairs AS
       (SELECT uuid_generate_v4() AS campaign_id,
               id                 AS question_id
          FROM questions),
     campaign_inserts AS
       (INSERT INTO campaigns (id, name, tenant_id)
         SELECT id_pairs.campaign_id, questions.title, tenant_id
           FROM questions
             JOIN id_pairs
                  ON questions.id = id_pairs.question_id)
INSERT
  INTO campaign_actions (campaign_id, question_id, app_area_id)
SELECT id_pairs.campaign_id, questions.id, questions.app_area_id
  FROM questions
    JOIN id_pairs
         ON questions.id = id_pairs.question_id;

Campaign_Inserts CTE基本上是用于插入到活动中的标签:它从未使用过.

Postgresql相关问答推荐

为什么Postgres在打印时能完全缩短时间跨度?

sqlalchemy在Flask 下运行时出现无法解释的错误

无法将 json 范围读取为 pgtype.Int4range

Select 与输入数组完全相交的所有行?

Ubuntu 上的 PostgreSQL 安装:找不到initdb命令

为什么在 PostgreSQL 中忽略查询超时?

PostgreSQL:在timestamp::DATE 上创建索引

我应该使用哪个 postgresql 包?

Nodemon - 安装期间clean exit - waiting for changes before restart

错误:syntax error at or near "user"

在 PostgreSQL 中将时间转换为秒

带有 postgres 的 DOCKER 容器,警告:could not open statistics file "pg_stat_tmp/global.stat": Operation not permitted

PostgreSQL:将时间戳转换为时间 - 或仅从时间戳列中检索时间

我不明白 postgresql 的 nextval() 是如何工作的,有人可以解释一下吗?

Postgres jsonbNOT contains运算符

Postgis 中 2 点之间的距离,单位为 4326 米

如何判断 PostgreSQL 事务中的待处理操作

将数据从 MS SQL 迁移到 PostgreSQL?

获取json列键为空的记录

GRANT SELECT 特权使用一个语句对所有序列