gpt4 book ai didi

ruby-on-rails - 更改 Rails 数据库中的列名。

转载 作者:搜寻专家 更新时间:2023-10-30 21:39:36 25 4
gpt4 key购买 nike

我有这张 table

class CreateEvents < ActiveRecord::Migration
def self.up
create_table :events do |t|
t.integer :subcategory
t.string :event_name
t.text :description
t.string :location
t.date :date
t.decimal :price

t.timestamps
end
end

def self.down
drop_table :events
end
end

我想将子类别更改为 subcategory_id。我尝试了这个,但没有用

ruby 脚本/生成迁移 RenameDatabaseColumn 然后我转到 db\migrate 中的文件并编辑为如下所示

class RenameDatabaseColumn < ActiveRecord::Migration
def self.up
rename_column :events, :subgategory, :subgategory_id
end

def self.down
# rename back if you need or do something else or do nothing
end
end

然后我运行命令 rake db:migrate 将列仍然是子类别。你能帮我吗?我正在使用 Rails 2.0

谢谢

最佳答案

您是否拼错了列名?不是 :subcategory 吗?你写了 :subgategory

class RenameDatabaseColumn < ActiveRecord::Migration
def self.up
rename_column :events, :subcategory, :subcategory_id
end

def self.down
# rename back if you need or do something else or do nothing
end
end

关于ruby-on-rails - 更改 Rails 数据库中的列名。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9436641/

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