运行迁移时出错

时间:2016-06-13 04:52:47

标签: ruby-on-rails ruby ruby-on-rails-4 migration

当我运行迁移时出现错误。我尝试删除主键列但没有任何工作

  Mysql2::Error: Can't create table 'new_gquestion.#sql-408_33' (errno: 150): ALTER TABLE `retailers` ADD CONSTRAINT `fk_rails_caa912f3c3`
FOREIGN KEY (`location_code`)
  REFERENCES `zusers` (`id`)
/home/techbirds/new-gquest/path/gems/mysql2-0.3.21/lib/mysql2/client.rb:80:in `_query'
/home/techbirds/new-gquest/path/gems/mysql2-0.3.21/lib/mysql2/client.rb:80:in `block in query'
/home/techbirds/new-gquest/path/gems/mysql2-0.3.21/lib/mysql2/client.rb:79:in `handle_interrupt'
/home/techbirds/new-gquest/path/gems/mysql2-0.3.21/lib/mysql2/client.rb:79:in `query'
/home/techbirds/new-gquest/path/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:299:in `block in execute'
/home/techbirds/new-gquest/path/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract_adapter.rb:473:in `block in log'
/home/techbirds/new-gquest/path/gems/activesupport-4.2.1/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/home/techbirds/new-gquest/path/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract_adapter.rb:467:in `log'
/home/techbirds/new-gquest/path/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:299:in `execute'
/home/techbirds/new-gquest/path/gems/activerecord-4.2.1/lib/active_record/connection_adapters/mysql2_adapter.rb:231:in `execute'
/home/techbirds/new-gquest/path/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/schema_statements.rb:748:in `add_foreign_key'
/home/techbirds/new-gquest/path/gems/activerecord-4.2.1/lib/active_record/migration.rb:662:in `block in method_missing'
/home/techbirds/new-gquest/path/gems/activerecord-4.2.1/lib/active_record/migration.rb:632:in `block in say_with_time'
/home/techbirds/new-gquest/path/gems/activerecord-4.2.1/lib/active_record/migration.rb:632:in `say_with_time'
/home/techbirds/new-gquest/path/gems/activerecord-4.2.1/lib/active_record/migration.rb:652:in `method_missing'
/home/techbirds/new-gquest/db/migrate/20160607052319_add_location_code_into_retailers.rb:5:in `change'
/home/techbirds/new-gquest/path/gems/activerecord-4.2.1/lib/active_record/migration.rb:606:in `exec_migration'
/home/techbirds/new-gquest/path/gems/activerecord-4.2.1/lib/active_record/migration.rb:590:in `block (2 levels) in migrate'
/home/techbirds/new-gquest/path/gems/activerecord-4.2.1/lib/active_record/migration.rb:589:in `block in migrate'
/home/techbirds/new-gquest/path/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:292:in `with_connection'
/home/techbirds/new-gquest/path/gems/activerecord-4.2.1/lib/active_record/migration.rb:588:in `migrate'
/home/techbirds/new-gquest/path/gems/activerecord-4.2.1/lib/active_record/migration.rb:765:in `migrate'
/home/techbirds/new-gquest/path/gems/activerecord-4.2.1/lib/active_record/migration.rb:995:in `block in execute_migration_in_transaction'
/home/techbirds/new-gquest/path/gems/activerecord-4.2.1/lib/active_record/migration.rb:1043:in `ddl_transaction'
/home/techbirds/new-gquest/path/gems/activerecord-4.2.1/lib/active_record/migration.rb:994:in `execute_migration_in_transaction'
/home/techbirds/new-gquest/path/gems/activerecord-4.2.1/lib/active_record/migration.rb:956:in `block in migrate'
/home/techbirds/new-gquest/path/gems/activerecord-4.2.1/lib/active_record/migration.rb:952:in `each'
/home/techbirds/new-gquest/path/gems/activerecord-4.2.1/lib/active_record/migration.rb:952:in `migrate'
/home/techbirds/new-gquest/path/gems/activerecord-4.2.1/lib/active_record/migration.rb:820:in `up'
/home/techbirds/new-gquest/path/gems/activerecord-4.2.1/lib/active_record/migration.rb:798:in `migrate'
/home/techbirds/new-gquest/path/gems/activerecord-4.2.1/lib/active_record/tasks/database_tasks.rb:137:in `migrate'
/home/techbirds/new-gquest/path/gems/activerecord-4.2.1/lib/active_record/railties/databases.rake:44:in `block (2 levels) in <top (required)>'
/home/techbirds/.rvm/gems/ruby-2.2.1/bin/ruby_executable_hooks:15:in `eval'
/home/techbirds/.rvm/gems/ruby-2.2.1/bin/ruby_executable_hooks:15:in `<main>'
ActiveRecord::StatementInvalid: Mysql2::Error: Can't create table 'new_gquestion.#sql-408_33' (errno: 150): ALTER TABLE `retailers` ADD CONSTRAINT `fk_rails_caa912f3c3`
FOREIGN KEY (`location_code`)
  REFERENCES `zusers` (`id`)


def change
    add_column :retailers,:location_code,:string
    add_index :zusers,:location_code
    add_foreign_key :retailers,:zusers,column: :location_code, primary_key: "location_code"
  end

1 个答案:

答案 0 :(得分:3)

我设置了一个新的Rails代码库来测试您的迁移,它运行良好。

请参阅回购here和迁移here

  

Mysql2 ::错误:无法创建表   'new_gquestion。#sql-408_33'(错误号:150):ALTER TABLE retailers ADD   约束fk_rails_caa912f3c3外键(location_code
  参考zusersid

我认为当您删除主键列规范时会发生此错误,但由于location_code是字符串类型而id是整数,所以它不起作用。 (如果未指定primary_key选项,则id为默认列。)

您指定primary_key选项的原因并不起作用,我不确定。我建议您检查一下location_code表中是否有zusers列。

您可以签出我的样本仓库提交https://github.com/larrylv/sf-37782027/commits/master,只需创建这两个表并使用迁移脚本添加外键。

相关问题