使用 PostgreSQL 进行 Prisma 迁移时发生外键违规

问题描述 投票:0回答:1

我有一个使用 Prisma 和 PostgreSQL 的项目。我正在尝试编写一个插入一些数据的迁移。该表有另一个表的外键。我插入的数据具有有效的外键(这些值存在于数据库中)。但是,在执行迁移时,我收到错误:

Error: P3006

Migration `20230922143649_test_prompt_creation` failed to apply cleanly to the shadow database.
Error:
insert or update on table "Prompt" violates foreign key constraint "Prompt_topicId_fkey"
   0: sql_schema_connector::validate_migrations
           with namespaces=None
             at schema-engine\connectors\sql-schema-connector\src\lib.rs:301
   1: schema_core::state::DevDiagnostic
             at schema-engine\core\src\state.rs:266

这是迁移脚本:

INSERT INTO
    "Prompt"("title", "desc", "seed", "promptHint", "topicId")
VALUES
    (
        '[REDACTED]',
        '[REDACTED]',
        '[REDACTED]',
        '[REDACTED]',
        2 -- Row in table Topic with id 2 exists
    );

直接针对数据库运行此脚本成功。

有什么想法为什么通过 Prisma 应用此迁移失败吗? 我假设它与影子数据库有关...需要注意的一项是主题行是通过种子文件创建的,而不是迁移,这可能是个问题。我不熟悉影子数据库的工作原理。

我尝试与 ChatGPT 合作调查潜在的解决方案,在谷歌上搜索类似问题,查看 Prisma GitHub 上的一些问题,并在 StackOverflow 中搜索类似问题。

postgresql database-migration prisma blitz.js
1个回答
0
投票

试试这个。它应该有效

插入“主题”(“id”,“other_columns”) 价值观 (2,“[已编辑]”), -- 根据需要添加更多行 (3,“[已编辑]”);

插入 "提示"("标题", "描述", "种子", "promptHint", "topicId") 价值观 ( '[已编辑]', '[已编辑]', '[已编辑]', '[已编辑]', 2 -- 引用 id 为 2 的现有主题 );

© www.soinside.com 2019 - 2024. All rights reserved.