在目标表不存在的情况下定义 FK

发布于 2024-12-22 18:35:35 字数 332 浏览 5 评论 0原文

当我直接在 CREATE 命令中创建具有 FK 定义的表并且目标表尚不存在时,会导致错误。

是否可以以某种方式暂停检查目标表是否存在?

我的 DBMS 是 Postgres。

示例(伪代码):

create table "Bar"
    foo_id integer FK of "Foo"."id",
    someattr text;

create table "Foo"
    id integer;

示例的顺序错误,这就是它无法运行的原因。

我正在尝试根据许多 sql 文件中的定义批量重新创建数据库。

When I create table that has definition of FK's directly in CREATE command and target table does not exists yet, results in error.

Can checking, if target table exists, be somehow suspended?

my DBMS is Postgres.

Example (pseudocode):

create table "Bar"
    foo_id integer FK of "Foo"."id",
    someattr text;

create table "Foo"
    id integer;

Example is in wrong order, thats why it wont run.

I'm trying to recreate databse in batch, based on definitions in many sql files.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷荒 2024-12-29 18:35:35

当我直接在 CREATE 命令中创建具有 FK 定义的表并且目标表尚不存在时,会导致错误。

是否可以以某种方式暂停检查目标表是否存在?

处理这个问题的最佳方法可能是:

  1. 以正确的顺序创建表,或者
  2. 创建约束
    在创建表之外,创建所有表之后。

When I create table that has definition of FK's directly in CREATE command and target table does not exists yet, results in error.

Can checking, if target table exists, be somehow suspended?

The best ways to deal with this are likely:

  1. Create your tables in the correct order, or
  2. Create the constraints
    outside the table creation, after all tables are created.
安人多梦 2024-12-29 18:35:35

暴力总是一种选择。

继续运行 DDL 脚本,直到运行没有错误为止。

更加优雅需要脚本的顺序结构。

添加存在检查是可能的,但我对 postgres 元数据不太熟悉。

Brute force is always an option.

Keep on running your DDL scripts in until you get a run with no errors.

More elegance requires a sequential structuring of your scripts.

Adding existence checks is possible, but I am not too familiar with the postgres metadata.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文