删除 PostgreSQL 中跨表的链接数据

发布于 2024-12-25 10:45:37 字数 342 浏览 4 评论 0原文

我正在寻找处理跨三个 PostgreSQL 表删除链接数据的最佳方法(技术上更多,但这是相同的想法)。

这三个表是agency、address 和agency_address。一个机构可以有多个地址,因此agency_address只是一个包含两列的链接表,agency_id和address_id(定义为各自的外键)

我想对其进行设置,以便在删除机构时它将删除链接表中的行Agency_address 和相关地址行自动生成。 (因此,如果一个机构被删除,它的所有地址也被删除)

我可以让它清除链接表,但不确定如何访问地址表。

(地址也是一个通用模型,将被其他人引用,就像一个有自己的链接表的“站点”。)

I'm looking for the best way to handle deleting linked data across three PostgreSQL tables (technically more, but it's the same idea).

The three tables are agency, address, and agency_address. An agency can have multiple addresses so the agency_address is just a link table with two columns, agency_id and address_id (defined as their respective foreign keys)

I want to set it up so that when an agency is deleted it will remove the link table row in agency_address and the related address row automagically. (So if an agency is deleted, so are all its addresses)

I can get it to clear the link table, but not sure how to get to the address table.

(Also address is a generic model and will be referenced by others, like a 'site' which will have their own link tables.)

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

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

发布评论

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

评论(1

青萝楚歌 2025-01-01 10:45:37

在表定义中使用带有 ON DELETE CASCADE 的外键。

ALTER TABLE agency_address
  ADD CONSTRAINT agency_address_agency_fkey FOREIGN KEY (agency_id)
      REFERENCES agency (agency_id) ON DELETE CASCADE;

似乎不确定您是否也应该自动删除地址。

如果是这样,您的数据模型是错误的,地址应该直接依赖于机构,具有与上面类似的外键约束,不需要 n:m 链接表。


了解更多信息后进行编辑:

因此地址可以链接到机构或网站,但不能同时链接到两者。

该模型可以按照您的方式工作,但您必须以某种方式确保链接到代理机构的地址也不会链接到网站。

addressagency_address 之间的外键约束指向“错误”方向,因此您不能简单地添加另一个 ON DELETE CASCADE。您可以使用额外的外键来实现它,但这很棘手。每个触发器的这种方法要简单得多:

CREATE OR REPLACE FUNCTION trg_agency_address_delaft()
  RETURNS trigger AS
$BODY$
BEGIN

DELETE FROM address
WHERE address_id = OLD.address_id;

END;
$BODY$
  LANGUAGE plpgsql;


CREATE TRIGGER delaft
  AFTER DELETE ON agency_address
  FOR EACH ROW EXECUTE PROCEDURE trg_agency_address_delaft();

有关触发器函数和<的更多信息手册中的 href="http://www.postgresql.org/docs/current/interactive/sql-createtrigger.html" rel="nofollow noreferrer">触发器。

Use foreign keys with ON DELETE CASCADE in the table definition.

ALTER TABLE agency_address
  ADD CONSTRAINT agency_address_agency_fkey FOREIGN KEY (agency_id)
      REFERENCES agency (agency_id) ON DELETE CASCADE;

It seems uncertain that you should delete addresses automatically, too.

If so, your data model is wrong and addresses should depend on agencies directly, with a foreign key constraint similar to the one above, no n:m linking table necessary.


Edit after more info:

So addresses can be linked to agencies or sites, but never to both at the same time.

The model could work as you have it, but you would have to make sure somehow that an address linked to an agency isn't linked to a site, too.

The foreign key constraint between address and agency_address points in the "wrong" direction, so you cannot simply add another ON DELETE CASCADE. You could implement it with an additional foreign key, but that's tricky. This approach per trigger is much simpler:

CREATE OR REPLACE FUNCTION trg_agency_address_delaft()
  RETURNS trigger AS
$BODY$
BEGIN

DELETE FROM address
WHERE address_id = OLD.address_id;

END;
$BODY$
  LANGUAGE plpgsql;


CREATE TRIGGER delaft
  AFTER DELETE ON agency_address
  FOR EACH ROW EXECUTE PROCEDURE trg_agency_address_delaft();

More about trigger functions and triggers in the manual.

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