【问题标题】:Getting error when running migrations运行迁移时出错
【发布时间】:2016-06-13 04:52:47
【问题描述】:

运行迁移时出现错误。我已尝试删除主键列,但没有任何效果

  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

【问题讨论】:

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


    【解决方案1】:

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

    查看 repo here 和迁移 here

    Mysql2::Error: 无法创建表 'new_gquestion.#sql-408_33' (errno: 150): ALTER TABLE retailers ADD 约束 fk_rails_caa912f3c3 外键 (location_code)
    参考zusers (id)

    我认为当您删除主键列规范时会发生此错误,并且它不起作用,因为location_code 是字符串类型而id 是整数。 (当您没有指定primary_key 选项时,id 是默认列。)

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

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

    【讨论】:

    • @halfelf :) 你是怎么找到我的?
    • 你的新 github 仓库
    • 看看谁来了~
    • id is integer 让我意识到我正在使用 gem 来使用 bigint 作为主键。我重新检查 gem 以找出解决方案。谢谢!
    猜你喜欢
    • 2017-12-17
    • 2016-08-17
    • 2015-04-06
    • 1970-01-01
    • 2011-03-13
    • 2020-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多