【问题标题】:Rails Foreigner: foreign key constraints for "test" DB using MySQLRails Foreigner:使用 MySQL 的“测试”数据库的外键约束
【发布时间】:2012-08-08 14:17:12
【问题描述】:

我正在尝试使用 Foreigner 在 Rails 中启用外键约束。它正在我的开发数据库上工作,但是当我尝试运行测试时出现以下错误:

Errors running test:units! #<ActiveRecord::StatementInvalid: Mysql2::Error: Can't create table 
'arizona_test.#sql-368_be' (errno: 150): ALTER TABLE `arizona_downloads` ADD CONSTRAINT 
`arizona_downloads_ibfk_1` FOREIGN KEY (`books_id`) REFERENCES `books`(id) ON DELETE SET NULL>

奇怪的是,测试运行良好,最后我得到了这个错误。

我怀疑 Foreigner 正在尝试对测试数据库使用 SQLite 语法,却不知道我正在使用 MySQL(和 mysql2 适配器)。有没有办法告诉老外我正在使用 MySQL 作为测试数据库?

【问题讨论】:

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


    【解决方案1】:

    仔细观察,我发现这显示了 MySQL errno 150。运行 SHOW ENGINE INNODB STATUS 显示:

    120808 11:24:29 Error in foreign key constraint of table arizona_test/#sql-368_6e:
     FOREIGN KEY (`book_id`) REFERENCES `books`(id) ON DELETE SET NULL:
    Cannot find an index in the referenced table where the
    referenced columns appear as the first columns, or column types
    in the table and the referenced table do not match for constraint.
    

    发生这种情况是因为我的开发环境使用 bigint(20) 作为 id,而 schema.rb 使用了 int(11),因为迁移依赖于 Rails 的默认 id 大小。

    为了解决这个问题,我更改了 default primary key column type,并重新进行了迁移。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-30
      • 2018-01-23
      • 2013-12-17
      • 2021-12-11
      • 1970-01-01
      • 2021-11-03
      相关资源
      最近更新 更多