内の参照を追加します。そして、それは私が、マイグレーションを発生させることの後に成功は、私は、MySQLデータベースに私の資格情報を使用して使用して新しいdemo_projectはOKだった私は、テーブルの私の使用してすくいデシベルを作成レール作成私の移行エラー
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
と最後の移行を作成
私はrake db:migrate
を実行したとき
rails g migration AddStyleToProducts style:references
この
class AddStyleToProducts < ActiveRecord::Migration[5.0]
def change
add_reference :products, :style, foreign_key: true
end
end
を作成するには、私はあなたがstyles
テーブルを持っていないエラー
== 20170213061527 CreateProducts: migrating
-- create_table(:products) -> 0.1451s == 20170213061527 CreateProducts: migrated (0.1453s) ==========================
== 20170213061805 AddQtySoldToProducts: migrating ============================= -- add_column(:products, :qty_solid, :integer) -> 0.3441s == 20170213061805 AddQtySoldToProducts: migrated (0.3443s) ====================
== 20170213061950 AddRefNumToProducts: migrating ============================== -- add_column(:products, :ref_num, :string) -> 0.0964s -- add_index(:products, :ref_num) -> 0.4156s == 20170213061950 AddRefNumToProducts: migrated (0.5121s) =====================
== 20170213062130 AddStyleToProducts: migrating =============================== -- add_reference(:products, :style, {:foreign_key=>true}) rake aborted! StandardError: An error has occurred, all later migrations canceled:
Mysql2::Error: Cannot add foreign key constraint: ALTER TABLE
products
ADD CONSTRAINTfk_rails_326a064677
FOREIGN KEY (style_id
) REFERENCESstyles
(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
block in query' /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
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: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
block in log' /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
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: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
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: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' /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' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:846: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
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:insay_with_time' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:835:in
method_missing' /Users/cdasia-tsd/Desktop/demo_project/db/migrate/20170213062130_add_style_to_products.rb:3:inchange' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:789:in
exec_migration' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:773: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
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:inwith_connection' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:771:in
migrate' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:951:inmigrate' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:1212: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:inddl_transaction' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:1211: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: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
each' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:1183:inmigrate_without_lock' /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/migration.rb:1299:inwith_advisory_lock' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:1132:in
migrate' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:1006:inup' /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/tasks/database_tasks.rb:161:inmigrate' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/railties/databases.rake:58:in
block (2 levels) in ' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/rake-12.0.0/exe/rake:27:in<top (required)>' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/bin/ruby_executable_hooks:15:in
eval' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/bin/ruby_executable_hooks:15:in<main>' ActiveRecord::StatementInvalid: Mysql2::Error: Cannot add foreign key constraint: ALTER TABLE
productsADD CONSTRAINT
fk_rails_326a064677FOREIGN KEY (
style_id) REFERENCES
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:inblock in query' /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:inquery' /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: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
instrument' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/connection_adapters/abstract_adapter.rb:583: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
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: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
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: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
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:inadd_reference' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:846: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: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
say_with_time' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:835:inmethod_missing' /Users/cdasia-tsd/Desktop/demo_project/db/migrate/20170213062130_add_style_to_products.rb:3:in
change' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:789:inexec_migration' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:773: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: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
with_connection' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:771:inmigrate' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:951:in
migrate' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:1212: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
ddl_transaction' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:1211: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
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:ineach' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:1183:in
migrate_without_lock' /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/migration.rb:1299:in
with_advisory_lock' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:1132:inmigrate' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:1006:in
up' /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/tasks/database_tasks.rb:161:in
migrate' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/railties/databases.rake:58:inblock (2 levels) in <top (required)>' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/rake-12.0.0/exe/rake:27:in
' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/bin/ruby_executable_hooks:15:ineval' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/bin/ruby_executable_hooks:15:in
' Mysql2::Error: Cannot add foreign key constraint /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
block in query' /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
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: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
block in log' /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
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: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
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: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' /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' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:846: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
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:insay_with_time' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:835:in
method_missing' /Users/cdasia-tsd/Desktop/demo_project/db/migrate/20170213062130_add_style_to_products.rb:3:inchange' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:789:in
exec_migration' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:773: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
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:inwith_connection' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:771:in
migrate' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:951:inmigrate' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:1212: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:inddl_transaction' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:1211: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: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
each' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:1183:inmigrate_without_lock' /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/migration.rb:1299:inwith_advisory_lock' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:1132:in
migrate' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/migration.rb:1006:inup' /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/tasks/database_tasks.rb:161:inmigrate' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/activerecord-5.0.1/lib/active_record/railties/databases.rake:58:in
block (2 levels) in ' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/gems/rake-12.0.0/exe/rake:27:in<top (required)>' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/bin/ruby_executable_hooks:15:in
eval' /Users/cdasia-tsd/.rvm/gems/ruby-2.3.3/bin/ruby_executable_hooks:15:in `' Tasks: TOP => db:migrate (See full trace by running task with --trace) TSD-Interns-MacBook-Pro:demo_project cdasia-tsd$
'styles'テーブルは既に存在しますか? –