这里我试图自定义mysql中的列名。 我已经使用 Rails 生成了模型
rails generate scaffold my_products product_category:string product_name:string
当我执行rake db:migrate
时,默认会生成ID。但我想将 id
列名称更改为 product_id
,这将与带主键的 ID 相同,以便于查询和其他操作。
我该怎么做?
或者我可以在生成模型时将 id 更改为 Product_id 吗?
最佳答案
这就是您的迁移文件的样子
class CreateMyProducts < ActiveRecord::Migration
def change
create_table :my_products, id: false do |t|
t.primary_key :product_id
t.string :product_category
t.string :product_name
t.timestamps null: false
end
end
end
确保添加id: false
并将primary_key设置为product_id
关于mysql - 如何在rails中生成模型时更改列名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31938935/