ActiveRecord迁移未执行 [英] ActiveRecord migration not executing

查看:79
本文介绍了ActiveRecord迁移未执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Rails的新手,并创建了一个自定义迁移来使用Rails Generate更改数据库结构.这是我发出的命令:rails g migration users.

I am new to rails, and created a custom migration to change my database structure using Rails Generate. Here is the command I issued: rails g migration users.

现在,在创建的文件中,我输入:

Now, in the file it created, I inputed:

class Users < ActiveRecord::Migration
  def change
    add_column :first_name
    add_column :last_name
    remove_column :name
  end
end

当我运行rake db:migrate时,什么都没有发生.我该怎么做才能解决此问题?

When I run rake db:migrate nothing happens. What do I need to do to fix this?

推荐答案

它根本没有运行吗?根据您提供的信息很难说.也许您应该尝试使用更独特的名称进行迁移?像这样:

It's not running at all? It's hard to say based on the info you gave. Perhaps you should try a migration with a more unique name? Something like:

rails g migration ConvertUsersNamesToSingleField

我不确定具有相同名称的两个迁移是否很酷.但是对于短的通用名称(如 Users ),这可能是这里的问题.为了后代和清楚起见,拥有一个冗长且描述性的迁移名称通常不会受到伤害.

I'm not sure if it's cool to have two migrations with the same name. But with short generic names like Users that might be the problem here. And it usually can't hurt to have a verbose and descriptive migration name, for posterity and clarity.

以下问题同意使用非唯一名称的迁移不起作用:使用同名

This questions agree that migration with non unique names don't work: Rails migrations with the same name

但是即使运行,这也会引发错误.您需要在这些列调用中包括表名,并且在创建字段时需要指定类型.

But even when ran, this will raise errors. You need to include table names in those column calls, and you need to specify a type when creating fields.

class ConvertUsersNamesToSingleField < ActiveRecord::Migration
  def change
    add_column :users, :first_name, :string
    add_column :users, :last_name, :string
    remove_column :users, :name
  end
end

这篇关于ActiveRecord迁移未执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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