在我的迁移错误

时间:2017-02-13 06:51:11

标签: ruby-on-rails ruby ruby-on-rails-3 activerecord

我使用mysql数据库创建rails new demo_project我的凭据是好的我使用rake db创建表:create

Created database 'demo_project_development'
Created database 'demo_project_test'

然后我生成一个表

rails g model Product name:string description:text inventory:integer 'price:decimal{7,2}' restocked_at:datetime

这是成功的

class CreateProducts < ActiveRecord::Migration[5.0]
  def change
    create_table :products do |t|
      t.string :name
      t.text :description
      t.integer :inventory
      t.decimal :price, precision: 7, scale: 2
      t.datetime :restocked_at

      t.timestamps
    end
  end
end

之后我生成迁移

rails g migration AddQtySoldToProducts qty_solid:integer

并创建此

class AddQtySoldToProducts < ActiveRecord::Migration[5.0]
  def change
    add_column :products, :qty_solid, :integer
  end
end

然后我创建另一个迁移

rails g migration AddRefNumToProducts ref_num:string:index

并创建此

class AddRefNumToProducts < ActiveRecord::Migration[5.0]
  def change
    add_column :products, :ref_num, :string
    add_index :products, :ref_num
  end
end

和上次迁移

rails g migration AddStyleToProducts style:references

并创建此

class AddStyleToProducts < ActiveRecord::Migration[5.0]
  def change
    add_reference :products, :style, foreign_key: true
  end
end

当我运行rake db:migrate时出现错误

  

== 20170213061527 CreateProducts:migrating

     

- create_table(:products) - &gt; 0.1451s   == 20170213061527 CreateProducts:migrated(0.1453s)==========================

     

== 20170213061805 AddQtySoldToProducts:migrating =============================    - add_column(:products,:qty_solid,:integer) - &gt; 0.3441s   == 20170213061805 AddQtySoldToProducts:migrated(0.3443s)====================

     

== 20170213061950 AddRefNumToProducts:migrating ==============================    - add_column(:products,:ref_num,:string) - &gt; 0.0964s    - add_index(:products,:ref_num) - &gt; 0.4156s   == 20170213061950 AddRefNumToProducts:migrated(0.5121s)=====================

     

== 20170213062130 AddStyleToProducts:migrating ===============================    - add_reference(:products,:style,{:foreign_key =&gt; true})rake aborted! StandardError:发生了错误,所有以后的迁移都发生了   取消:

     

Mysql2 ::错误:无法添加外键约束:ALTER TABLE   products添加约束fk_rails_326a064677外键   (style_id)参考stylesid)   /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/mysql2-0.4.5/lib/mysql2/client.rb:120:in   _query' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/mysql2-0.4.5/lib/mysql2/client.rb:120:in 阻止查询&#39;   /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/mysql2-0.4.5/lib/mysql2/client.rb:119:in   handle_interrupt' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/mysql2-0.4.5/lib/mysql2/client.rb:119:in 的查询&#39;   /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:218:in   block in execute' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/connection_adapters/abstract_adapter.rb:589:in阻止日志记录   /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activesupport-5.0.1/lib/active_support/notifications/instrumenter.rb:21:in   instrument' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/connection_adapters/abstract_adapter.rb:583:in登录&#39;   /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:218:in   execute' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/connection_adapters/mysql/database_statements.rb:31:in 执行&#39;   /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/connection_adapters/abstract/schema_statements.rb:916:in   add_foreign_key' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/connection_adapters/abstract/schema_definitions.rb:623:in foreign_key&#39;   /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/connection_adapters/abstract/schema_definitions.rb:100:in   add_to' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/connection_adapters/abstract/schema_statements.rb:826:in add_reference&#39;   /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:846:in   {_ 1}}阻止在say_with_time&#39;   /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:815:in   block in method_missing' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:815:in 的method_missing&#39;   /Users/cdasia-tsd/Desktop/demo_project/db/migrate/20170213062130_add_style_to_products.rb:3:in   say_with_time' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:835:in exec_migration&#39;   /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:773:in   change' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:789:in 阻止迁移&#39;   /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:398:in   block (2 levels) in migrate' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:772:in 迁移&#39;   /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:951:in   execute_migration_in_transaction&#39;中的with_connection' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:771:in 阻止   /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:1282:in   migrate' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:1212:in execute_migration_in_transaction&#39;   /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:1184:in   ddl_transaction' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:1211:in 各&#39;   /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:1183:in   block in migrate_without_lock' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:1183:in 阻止迁移&#39;   /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:1299:in   migrate_without_lock' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:1132:in 迁移&#39;   /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:1006:in   with_advisory_lock' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:1132:in 迁移&#39;   /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/tasks/database_tasks.rb:161:in   up' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:984:in 阻止(2级)&#39;   /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/rake-12.0.0/exe/rake:27:in   migrate' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/railties/databases.rake:58:in 的eval&#39;   /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/bin/ruby_executable_hooks:15:in   <top (required)>' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/bin/ruby_executable_hooks:15:in {产品{1}} {fk_rails_326a064677 {1}} {style_id {1}} {样式{1}} ID为<main>' ActiveRecord::StatementInvalid: Mysql2::Error: Cannot add foreign key constraint: ALTER TABLE _查询&#39;   /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/mysql2-0.4.5/lib/mysql2/client.rb:120:in   ADD CONSTRAINT handle_interrupt&#39;   /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/mysql2-0.4.5/lib/mysql2/client.rb:119:in   FOREIGN KEY (阻止执行&#39;   /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/connection_adapters/abstract_adapter.rb:589:in ) REFERENCES instrument&#39;   /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/connection_adapters/abstract_adapter.rb:583:in (执行&#39;   /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/connection_adapters/mysql/database_statements.rb:31:in   ) /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/mysql2-0.4.5/lib/mysql2/client.rb:120:in add_foreign_key&#39;   /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/connection_adapters/abstract/schema_definitions.rb:623:in   block in query' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/mysql2-0.4.5/lib/mysql2/client.rb:119:in add_to&#39;   /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/connection_adapters/abstract/schema_statements.rb:826:in   method_missing&#39;中的query' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:218:in 阻止   /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:815:in   block in log' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activesupport-5.0.1/lib/active_support/notifications/instrumenter.rb:21:in say_with_time&#39;   /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:835:in   log' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:218:in 变化&#39;   /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:789:in   execute' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/connection_adapters/abstract/schema_statements.rb:916:in 阻止(2级)迁移&#39;   /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:772:in   foreign_key' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/connection_adapters/abstract/schema_definitions.rb:100:in with_connection&#39;   /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:771:in   add_reference' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:846:in 迁移&#39;   /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:1212:in   block in say_with_time' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:815:in ddl_transaction&#39;   /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:1211:in   {_ 1}}阻止了migrate_without_lock&#39;   /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:1183:in   method_missing' /Users/cdasia-tsd/Desktop/demo_project/db/migrate/20170213062130_add_style_to_products.rb:3:in migrate_without_lock&#39;   /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:1132:in   exec_migration' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:773:in with_advisory_lock&#39;   /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:1132:in   block in migrate' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:398:in 起来&#39;   /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:984:in   migrate' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:951:in 迁移&#39;   /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/railties/databases.rake:58:in   block in execute_migration_in_transaction' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:1282:in &#39;   /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/bin/ruby_executable_hooks:15:in   execute_migration_in_transaction' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:1184:in &#39; Mysql2 ::错误:无法添加外键约束   /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/mysql2-0.4.5/lib/mysql2/client.rb:120:in   each' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:1183:in 阻止查询&#39;   /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/mysql2-0.4.5/lib/mysql2/client.rb:119:in   block in migrate' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:1299:in 的查询&#39;   /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:218:in   migrate' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:1006:in 阻止日志记录   /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activesupport-5.0.1/lib/active_support/notifications/instrumenter.rb:21:in   migrate' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/tasks/database_tasks.rb:161:in 登录&#39;   /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:218:in   block (2 levels) in <top (required)>' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/rake-12.0.0/exe/rake:27:in 执行&#39;   /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/connection_adapters/abstract/schema_statements.rb:916:in   eval' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/bin/ruby_executable_hooks:15:in foreign_key&#39;   /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/connection_adapters/abstract/schema_definitions.rb:100:in   _query' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/mysql2-0.4.5/lib/mysql2/client.rb:120:in add_reference&#39;   /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:846:in   {_ 1}}阻止在say_with_time&#39;   /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:815:in   handle_interrupt' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/mysql2-0.4.5/lib/mysql2/client.rb:119:in 的method_missing&#39;   /Users/cdasia-tsd/Desktop/demo_project/db/migrate/20170213062130_add_style_to_products.rb:3:in   block in execute' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/connection_adapters/abstract_adapter.rb:589:in exec_migration&#39;   /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:773:in   instrument' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/connection_adapters/abstract_adapter.rb:583:in阻止迁移&#39;   /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:398:in   execute' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/connection_adapters/mysql/database_statements.rb:31:in 迁移&#39;   /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:951:in   execute_migration_in_transaction&#39;中的add_foreign_key' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/connection_adapters/abstract/schema_definitions.rb:623:in 阻止   /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:1282:in   add_to' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/connection_adapters/abstract/schema_statements.rb:826:in execute_migration_in_transaction&#39;   /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:1184:in   block in method_missing' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:815:in 各&#39;   /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:1183:in   say_with_time' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:835:in 阻止迁移&#39;   /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:1299:in   change' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:789:in 迁移&#39;   /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:1006:in   block (2 levels) in migrate' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:772:in 迁移&#39;   /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/tasks/database_tasks.rb:161:in   with_connection' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:771:in 阻止(2级)&#39;   /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/rake-12.0.0/exe/rake:27:in   migrate' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:1212:in 的eval&#39;   /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/bin/ruby_executable_hooks:15:in   `&#39;任务:TOP =&gt; db:migrate(通过运行任务查看完整跟踪   --trace)TSD-Interns-MacBook-Pro:demo_project cdasia-tsd $

2 个答案:

答案 0 :(得分:1)

您没有styles表。

答案 1 :(得分:0)

是的,在运行此迁移之前,您应该创建样式表,否则您会遇到同样的问题。

请看下面的链接可能会对你有帮助 -

add_reference link1

add_reference link2

注意:如果您在任何地方编写add_reference,那么请确保首先存在此表,或者如果您想要一起创建所有迁移,那么迁移顺序应该是参考表创建的第一次迁移 之后运行具有add_reference的迁移。