function - Postgres ON DELETE CASCADE 会在后续表上触发 ON DELETE 触发器吗?

标签 function postgresql triggers

ON DELETE CASCADE 会在后续表上触发 ON DELETE 触发器吗?

我有三个表:结果、m_int 和行计数。 M_int 与 FK 的结果相关。结果设置了 ON DELETE CASCADE,并且 m_int 有一个 BEFORE DELETE 触发器。 Rowcount 跟踪 m_int 中正在使用的行数以及由哪个用户使用。

我的问题是,当我从结果中删除一行时,我希望 DELETE CASCADE 触发 m_int 上的触发器。有任何想法吗?我花了整个上午阅读文档,但找不到答案。

peri=> \d results
                          Table "public.results"
  Column   |  Type   |                      Modifiers                      
-----------+---------+-----------------------------------------------------
 result_id | integer | not null default nextval('result_id_seq'::regclass)
 trial__id | integer | not null
 title     | text    | 
Indexes:
    "results_pkey" PRIMARY KEY, btree (result_id)
Referenced by:
    TABLE "m_int" CONSTRAINT "m_int_result__id_fkey" FOREIGN KEY (result__id) REFERENCES results(result_id) ON DELETE CASCADE

peri=> \d m_int
                               Table "public.m_int"
   Column   |  Type   |                         Modifiers                          
------------+---------+------------------------------------------------------------
 metric__id | integer | not null
 result__id | integer | not null
 value      | integer | not null
 m_value_id | bigint  | not null default nextval('m_int_m_value_id_seq'::regclass)
Indexes:
    "m_int_pkey" PRIMARY KEY, btree (m_value_id)
Foreign-key constraints:
    "m_int_metric__id_fkey" FOREIGN KEY (metric__id) REFERENCES metrics(metric_id) ON DELETE CASCADE
    "m_int_result__id_fkey" FOREIGN KEY (result__id) REFERENCES results(result_id) ON DELETE CASCADE
Triggers:
    addrows_m_int AFTER INSERT ON m_int FOR EACH ROW EXECUTE PROCEDURE add_rows_m_int()
    remrows_m_int BEFORE DELETE ON m_int FOR EACH ROW EXECUTE PROCEDURE rem_rows_m_int()

peri=> \d rowcount
     Table "public.rowcount"
   Column   |  Type   | Modifiers 
------------+---------+-----------
 user__id   | integer | not null
 table_name | text    | not null
 total_rows | bigint  | 
Indexes:
    "rowcount_pkey" PRIMARY KEY, btree (user__id, table_name)

最佳答案

CREATE TABLE 的手册会告诉你:

In addition, when the data in the referenced columns is changed, certain actions are performed on the data in this table's columns. The ON DELETE clause specifies the action to perform when a referenced row in the referenced table is being deleted. [...]

所以是的,删除 results 也会删除 m_int 中的内容。 当然,这并不意味着删除 m_int 会删除 results 中的任何内容。 您的额外触发器 add_rows_m_intrem_rows_m_int 会执行或阻止什么操作 - 我们不知道。

关于function - Postgres ON DELETE CASCADE 会在后续表上触发 ON DELETE 触发器吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9931600/

相关文章:

python - Spark join 抛出 'function' object has no attribute '_get_object_id' 错误。我该如何解决?

java - 在名称由字符串变量定义的类中调用已知函数(带参数)

postgresql - 错误 : operator does not exist: character varying = numeric

postgresql - 使用 PostgreSQL 的 PIVOT

.net - XAML:为什么这个触发器不起作用?

function - 在lua中组合两个函数

javascript - 为什么非静态变量的行为类似于静态变量?

django - 避免 Django 迁移问题的技术?

mysql触发函数使用日期添加函数

javascript - 在 javascript 中,将自定义事件附加到文档本身是不好的做法吗?