更改Rails数据库中的列名。 [英] Change a column name in rails database.

查看:107
本文介绍了更改Rails数据库中的列名。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此表

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。我尝试了这个,但是不起作用

and i want to change the subcategory to subcategory_id. I tries this one but is not working

红宝石脚本/生成迁移RenameDatabaseColumn,然后我转到db\migrate中的文件并进行编辑,使其看起来像这样

ruby script/generate migration RenameDatabaseColumn and then i went to the file which is in db\migrate and edited to look like this

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

then i run the command rake db:migrate put the column is still subcategory. Can you help me please? I'm using rails 2.0

谢谢

推荐答案

拼写错误的列名? :subcategory 是吗?您写了:subgategory

Did you misspell the column name? isn't it :subcategory? You wrote :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

这篇关于更改Rails数据库中的列名。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆