2012-08-08 38 views
0

爲「測試」 DB外鍵約束,我試圖用Foreigner,以使在Rails的外鍵約束。它的工作對我的開發數據庫,​​但我得到以下錯誤,當我嘗試運行我的測試:Rails的外國人:使用MySQL

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> 

奇怪的是,測試運行正常,我在最後得到這個錯誤。

我懷疑外國人試圖使用SQLite語法測試數據庫,不知道我正在使用MySQL(和mysql2適配器)。有沒有辦法來告訴我使用MySQL的測試數據庫外國人?

回答

0

仔細看,我認爲這是否顯示MySQL的錯誤號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),IDS,而schema.rb具有INT(11),由於該上Rails的默認ID大小依賴遷移。

爲了解決這個問題,我改變了default primary key column type,並重新做了我的遷移。