gpt4 book ai didi

ruby-on-rails - 如何格式化此 PostgreSQL 查询以便能够与 db :migrate 一起运行

转载 作者:行者123 更新时间:2023-12-04 07:36:26 24 4
gpt4 key购买 nike

我想创建下表。我通过使用 ActiveRecord 运行 SQL 来实现它

alter table items add column sumup numeric generated always as ( quantity * price + (quantity * price* fpa/100) ) stored;

这是正确的做法吗?

  def change
create_table :items do |t|
t.string :itemName
t.string :prom
t.string :promCode
t.string :baseCode
t.string :desc
t.numeric :fpa
t.numeric :price
t.integer :quantity
t.string :monadaMe
t.string :familys

t.timestamps
end
ActiveRecord::Base.connection.execute(" alter table items add column sumup numeric generated always as ( quantity * price + (quantity * price* fpa/100) ) stored;")
end
end

最佳答案

只创建新列:

rails generate migration AddSumupToItems sumup:float

在您的模型中,添加:

before_save :calculate_sumup

def calculate_sumup
sumup = ( quantity * price + (quantity * price* fpa/100) )
self.sumup = sumup
end

关于ruby-on-rails - 如何格式化此 PostgreSQL 查询以便能够与 db :migrate 一起运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60922051/

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