mysql - 运行所有迁移文件时出现问题。还能做什么?

标签 mysql ruby-on-rails

我有大量迁移文件需要运行,其中一些显然已过时。例如:

这失败了:

  def self.up
    create_table(:users) do |t|
      t.database_authenticatable :null => false
      ...

出现此错误:

-- create_table(:users)
rake aborted!
StandardError: An error has occurred, all later migrations canceled:

undefined method `database_authenticatable' for #<ActiveRecord::ConnectionAdapters::TableDefinition

我猜是因为 Devise 在撰写本文时已更新。

我还能做什么?

原来有一个模式文件。这是其中的一部分:

ActiveRecord::Schema.define(version: 20160221211458) do

  create_table "activities", force: true do |t|
    t.integer  "user_id",                  null: false
    t.string   "target_type", default: "", null: false
    t.integer  "target_id",                null: false
    t.string   "action",      default: "", null: false
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  add_index "activities", ["target_id"], name: "index_activities_on_target_id_and_target_type", using: :btree
  add_index "activities", ["user_id"], name: "index_activities_on_user_id", using: :btree

  create_table "addresses", force: true do |t|
  ...

执行 bundle exec rake db:schema:load 与尝试运行迁移之间有什么区别。两者有区别吗?

另外,这个数据库是mysql数据库:

# SQLite version 3.x
#   gem install sqlite3-ruby (not necessary on OS X Leopard)
development:
  adapter: mysql2
  database: arthouse_development
  username: root
  password:
  host: localhost
  port: 3306
  #socket: /tmp/mysql.sock

运行 bundle exec rake db:schema:load 后如何查看此数据库

最佳答案

This fails:

def self.up
 create_table(:users) do |t|
 t.database_authenticatable :null => false

I assume because Devise was updated at the time of writing this.

我需要知道您收到的错误消息

It turns out there's a schema file:

在您的架构中,我看到您没有users 表,我认为您是need to check your db mysql2看看你那里有没有东西。

What's the difference between doing bundle exec rake db:schema:load vs trying to run the migrations. Is there a difference between the two?

假设我 fork 了一个 github 项目并且 schema 文件已经填充。使用该命令,我加载了架构,因此我不会运行每次迁移,而只是像在架构中一样创建表。

迁移使用脚本来填充数据库,此脚本的功能类似于 add_column修改 schema.rb 文件。

ActiveRecord:Migrations 中的每个函数将修改架构,这意味着可能会添加一列然后删除。迁移中的这种冲突是由于迁移中的不兼容引起的,例如您添加了一个列,然后您需要将其反转,但是后来您为该列添加了索引,因此迁移失败。更多信息在下面link

Migrations are a convenient way to alter your database schema over time in a consistent and easy way. They use a Ruby DSL so that you don't have to write SQL by hand, allowing your schema and changes to be database independent.

You can think of each migration as being a new 'version' of the database. A schema starts off with nothing in it, and each migration modifies it to add or remove tables, columns, or entries. Active Record knows how to update your schema along this timeline, bringing it from whatever point it is in the history to the latest version. Active Record will also update your db/schema.rb file to match the up-to-date structure of your database.

解决方案

你的错误

rake aborted!
StandardError: An error has occurred, all later migrations canceled:

undefined method `database_authenticatable' for #<ActiveRecord::ConnectionAdapters::TableDefinition

鉴于您的问题,您应该找到解决方案

undefined method `database_authenticatable' for #<ActiveRecord::ConnectionAdapters::TableDefinition:0x9dee690>

关于mysql - 运行所有迁移文件时出现问题。还能做什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47079901/

相关文章:

mysql - Mysql 语法错误

ruby-on-rails - 计算数据库用户数据的大量统计信息 : optimizing performance

ruby-on-rails - 使用 FactoryGirl 测试简单的 STI

php - 我如何使用 PHP 以用户先前提交的答案的形式为动态表播种?

php - 事务中的多个查询并使用 php 提交/回滚

mysql - 警告 不安全语句写入二进制日志 using 语句

ruby-on-rails - 混合 ActiveRecord 查找条件

ruby-on-rails - 为什么在尝试使用 postgresql 作为 RoR 中的数据库时会出现此错误?

ruby-on-rails - Rails3 载波未上传

mysql - 无法在mysql表中插入一条以自增键为PK的记录