postgresql postgres 中的 ON DELETE SET NULL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19898274/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 01:10:59  来源:igfitidea点击:

ON DELETE SET NULL in postgres

entity-frameworkpostgresql

提问by tamikoon

HI I'm new to postgresql environment so been lost for a while. I want to keep my data when parent entity is deleted. I want to know how to make 'ON DELETE SET NULL' for postgresql database. Please give me a clue.

嗨,我是 postgresql 环境的新手,所以迷路了一段时间。我想在删除父实体时保留我的数据。我想知道如何为 postgresql 数据库制作“ON DELETE SET NULL”。请给我一个线索。

回答by Craig Ringer

ON DELETE SET NULLis a standard foreign key constraint option.

ON DELETE SET NULL是标准的外键约束选项。

CREATE TABLE some_child (
    parent_id integer references parent(id) on delete set null
);

or:

或者:

ALTER TABLE some_child 
ADD CONSTRAINT parent_id_fk 
FOREIGN KEY (parent_id) REFERENCES parent(id) 
ON DELETE SET NULL;

See the documentation.

请参阅文档

In future posts make sure you include your PostgreSQL version and explain what you've already tried.

在以后的帖子中,请确保包含您的 PostgreSQL 版本并解释您已经尝试过的内容。