gpt4 book ai didi

sql - 从 before_create 移动到 PostgreSql 触发器?

转载 作者:行者123 更新时间:2023-11-29 12:20:25 24 4
gpt4 key购买 nike

我们有这样的带有 before_create 触发器的模型:

def set_location_id
self.location_id = Location.find_by(code: location_code).id
end

def set_product_id
self.product_id = Product.find_by(code: product_code).id
end

您认为将这些 Rails 触发器移至 PostgreSql 是个好主意吗?

性能怎么样?

最佳答案

使用 PostgreSQL 的插入触发器之后或之前肯定比 Ruby(activerecord) before_create 更快更高效。但是您的代码将变得非常难以维护。现在您的软件业务逻辑将位于两个不同的位置1) rails 应用程序中的存储过程2) 在你的 rails 模型中

有两个不同的位置会使以后的调试和编程变得困难。

关于sql - 从 before_create 移动到 PostgreSql 触发器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28762957/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com