运行 Ruby on Rails 数据库迁移时出错

发布于 2024-11-07 06:45:59 字数 2362 浏览 3 评论 0原文

我正在 Ruby on Rails 应用程序中安装用于身份验证的 devise gem,并像这样运行数据库迁移:

rake db:migrate

并收到此错误:

undefined method `reference' for #<ActiveRecord::ConnectionAdapters::TableDefinition:0x9322248>

这有点神秘。我应该去哪里调试这个问题,可能是什么问题?

我做的唯一非标准的事情是给它表名“users”,这是我在上一个命令中的表名:rails生成设计用户

此外,我的routes.rb文件有这个新条目:

devise_for :users

可能问题是不匹配的我的数据库中的列以及 auth 包认为用户表应该是什么样子。我在哪里可以看到 auth 包认为这些列是什么样的?我在哪里可以找到我拥有的用户表的 create-table 命令的位置。它最初是用脚手架命令制作的,它在我的系统中放置了一大堆额外且无用的东西。

我的 db/migrate/users/create_users 文件如下所示:

class CreateUsers < ActiveRecord::Migration
  def self.up
    create_table :users do |t|

      t.timestamps
    end
  end

  def self.down
    drop_table :users
  end
end

这是基本的,但数据库中的用户表具有以下列:

+------------------+------------------+------+-----+---------+-------+
| Field            | Type             | Null | Key | Default | Extra |
+------------------+------------------+------+-----+---------+-------+
| uid              | int(10) unsigned | NO   | PRI | 0       |       |
| name             | varchar(60)      | NO   | UNI |         |       |
| pass             | varchar(128)     | NO   |     |         |       |
| mail             | varchar(254)     | YES  | MUL |         |       |
| theme            | varchar(255)     | NO   |     |         |       |
| signature        | varchar(255)     | NO   |     |         |       |
| signature_format | varchar(255)     | YES  |     | NULL    |       |
| created          | int(11)          | NO   | MUL | 0       |       |
| access           | int(11)          | NO   | MUL | 0       |       |
| login            | int(11)          | NO   |     | 0       |       |
| status           | tinyint(4)       | NO   |     | 0       |       |
| timezone         | varchar(32)      | YES  |     | NULL    |       |
| language         | varchar(12)      | NO   |     |         |       |
| picture          | int(11)          | NO   |     | 0       |       |
| init             | varchar(254)     | YES  |     |         |       |
| data             | longblob         | YES  |     | NULL    |       |
+------------------+------------------+------+-----+---------+-------+

我不确定运行 migrate 命令后如何存在这种不一致。如果不是我发布的上述文件,它从哪里获取指令?

谢谢!

I am installing the devise gem for authentication in a Ruby on Rails application and I ran the database migration like this:

rake db:migrate

and got this error:

undefined method `reference' for #<ActiveRecord::ConnectionAdapters::TableDefinition:0x9322248>

This is a bit cryptic. Where should I go to debug this and what could the problem be?

The only non-standard thing I did was give it the table name "users" which is my table name in this previous command: rails generate devise users

Also, my routes.rb file has this new entry:

devise_for :users

Probably the issue is mis-matched columns in my database and what the auth package thinks the users table should be like. Where do I look to see what the auth package thinks the columns are like? And where do I find where the create-table command is for the users table that I have. It was made with the scaffold command originally which put a whole bunch of extra and useless things in my system.

My db/migrate/users/create_users file looks like this:

class CreateUsers < ActiveRecord::Migration
  def self.up
    create_table :users do |t|

      t.timestamps
    end
  end

  def self.down
    drop_table :users
  end
end

Which is basic, but my users table in the db has these columns:

+------------------+------------------+------+-----+---------+-------+
| Field            | Type             | Null | Key | Default | Extra |
+------------------+------------------+------+-----+---------+-------+
| uid              | int(10) unsigned | NO   | PRI | 0       |       |
| name             | varchar(60)      | NO   | UNI |         |       |
| pass             | varchar(128)     | NO   |     |         |       |
| mail             | varchar(254)     | YES  | MUL |         |       |
| theme            | varchar(255)     | NO   |     |         |       |
| signature        | varchar(255)     | NO   |     |         |       |
| signature_format | varchar(255)     | YES  |     | NULL    |       |
| created          | int(11)          | NO   | MUL | 0       |       |
| access           | int(11)          | NO   | MUL | 0       |       |
| login            | int(11)          | NO   |     | 0       |       |
| status           | tinyint(4)       | NO   |     | 0       |       |
| timezone         | varchar(32)      | YES  |     | NULL    |       |
| language         | varchar(12)      | NO   |     |         |       |
| picture          | int(11)          | NO   |     | 0       |       |
| init             | varchar(254)     | YES  |     |         |       |
| data             | longblob         | YES  |     | NULL    |       |
+------------------+------------------+------+-----+---------+-------+

And I am not sure how such an inconsistency can exist after I run the migrate command. Where does it take instructions from if not the above file I posted?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

世界等同你 2024-11-14 06:45:59

生成新模型后,我遇到了类似的错误:

rails generate model Status open:boolean available:integer station:reference

问题是我在生成模型时使用“引用”而不是“引用”。此命令创建以下迁移:

class CreateStatuses < ActiveRecord::Migration
    def change
        create_table :statuses do |t|
            t.boolean :open
            t.integer :available
            t.reference :station
        end
    end
end

并且方法“引用”未定义,因此出现错误。我的情况下的迁移应该是:

class CreateStatuses < ActiveRecord::Migration
    def change
        create_table :statuses do |t|
            t.boolean :open
            t.integer :available
            t.references :station
        end
    end
end

I had a similar error after generating a new model:

rails generate model Status open:boolean available:integer station:reference

The problem was I was using 'reference' instead of 'references' when generating the model. This command creates the following migration:

class CreateStatuses < ActiveRecord::Migration
    def change
        create_table :statuses do |t|
            t.boolean :open
            t.integer :available
            t.reference :station
        end
    end
end

And the method 'reference' is undefined, thus the error. The migration in my case should be:

class CreateStatuses < ActiveRecord::Migration
    def change
        create_table :statuses do |t|
            t.boolean :open
            t.integer :available
            t.references :station
        end
    end
end
梅窗月明清似水 2024-11-14 06:45:59

我建议您使用 --trace 选项运行 db:migrate 命令:

rake db:migrate --trace

作为示例,我故意在我的设备迁移中添加了语法错误,这是我得到的输出的片段。正如您所看到的,--trace 选项应该指出确切的错误(迁移文件+行号)。

    undefined method `strin' for #<ActiveRecord::ConnectionAdapters::TableDefinition:0x00000106c5ea98>
    /Users/#####/.rvm/gems/ruby-1.9.2-p136@rails3/gems/activerecord-3.0.5/lib/active_record/connection_adapters/abstract/schema_definitions.rb:326:in `method_missing'
    /Users/#####/rails/$$$/db/migrate/20101031153010_devise_create_users.rb:13:in `block in up'
    /Users/#####/.rvm/gems/ruby-1.9.2-p136@rails3/gems/activerecord-3.0.5/lib/active_record/connection_adapters/abstract/schema_statements.rb:157:in `create_table'
...

请注意,您的迁移文件位于 db/migrate 目录下。因此,鉴于上述错误,我需要打开 db/migrate/20101031153010_devise_create_users.rb 迁移文件并修复第 13 行的错误。

I suggest you run the db:migrate command with the --trace option:

rake db:migrate --trace

As an example, I purposely added a syntax error in my devise migration and this is a fragment of the output I got. As you can see, the --trace option should point you to the exact error (migration file + line #).

    undefined method `strin' for #<ActiveRecord::ConnectionAdapters::TableDefinition:0x00000106c5ea98>
    /Users/#####/.rvm/gems/ruby-1.9.2-p136@rails3/gems/activerecord-3.0.5/lib/active_record/connection_adapters/abstract/schema_definitions.rb:326:in `method_missing'
    /Users/#####/rails/$$$/db/migrate/20101031153010_devise_create_users.rb:13:in `block in up'
    /Users/#####/.rvm/gems/ruby-1.9.2-p136@rails3/gems/activerecord-3.0.5/lib/active_record/connection_adapters/abstract/schema_statements.rb:157:in `create_table'
...

Note that your migration files are located under the db/migrate directory. So given the above error, I would need to open up the db/migrate/20101031153010_devise_create_users.rb migration file and fix the error on line 13.

一笔一画续写前缘 2024-11-14 06:45:59

检查您的迁移文件“migrate/20101031153010_devise_create_users.rb”。
您可能在代码中犯了错误或拼写错误。

Check your migration file "migrate/20101031153010_devise_create_users.rb".
You have probably made a mistake or a typo in the code.

梦里的微风 2024-11-14 06:45:59

我的问题是我使用了 :

za$ rails g scaffold team name:string team_id:integer:uniq  references:vendor

而不是 :

za$ rails g scaffold team name:string team_id:integer:uniq  vendor:references

只是将 vendor:references 更改为vendor:references

愚蠢的错误,我知道。

My issue was that I used :

za$ rails g scaffold team name:string team_id:integer:uniq  references:vendor

Instead of :

za$ rails g scaffold team name:string team_id:integer:uniq  vendor:references

just changed vendor:references to vendor:references

Stupid mistake, I know.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文