我使用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
)参考styles
(id
) /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:inhandle_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:inblock 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:ininstrument' /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:inexecute' /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:inadd_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:inadd_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:inblock 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:insay_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:inchange' /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:inblock (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:inmigrate' /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:inddl_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:inblock 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:inmigrate_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:inwith_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:inup' /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:inmigrate' /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:inADD CONSTRAINT
handle_interrupt&#39; /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/mysql2-0.4.5/lib/mysql2/client.rb:119:inFOREIGN 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:inblock 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:inblock 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:inlog' /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:inexecute' /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:inforeign_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:inadd_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:inblock 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:inmethod_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:inexec_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:inblock 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:inmigrate' /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:inblock 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:inexecute_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:ineach' /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:inblock 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:inmigrate' /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:inmigrate' /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:inblock (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:ineval' /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:inhandle_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:inblock 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:ininstrument' /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:inexecute' /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:inadd_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:inblock 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:insay_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:inchange' /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:inblock (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:inwith_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:inmigrate' /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 $
答案 0 :(得分:1)
您没有styles
表。
答案 1 :(得分:0)
是的,在运行此迁移之前,您应该创建样式表,否则您会遇到同样的问题。
请看下面的链接可能会对你有帮助 -
注意:如果您在任何地方编写add_reference,那么请确保首先存在此表,或者如果您想要一起创建所有迁移,那么迁移顺序应该是参考表创建的第一次迁移 之后运行具有add_reference的迁移。