在目标表不存在的情况下定义 FK
当我直接在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
处理这个问题的最佳方法可能是:
在创建表之外,创建所有表之后。
The best ways to deal with this are likely:
outside the table creation, after all tables are created.
暴力总是一种选择。
继续运行 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.