2016-11-24 2 views
1

が発生し、私は、次の続編モデルにDatabaseCleanerとRSpecのテストを実行しているに続編にdatabase_cleanerとRSpecのは、外部キーの打ち切り誤差

class User < Sequel::Model 
     one_to_many :memberships 
     many_through_many :accounts, [[:memberships, :user_id, :account_id]] 
end 

class Account < Sequel::Model 
     one_to_many :memberships 
     many_through_many :users, [[:memberships, :user_id, :account_id]] 
end 

class Membership < Sequel::Model 
     many_to_one :account 
     many_to_one :user 
end 

私がテストを実行したときに、私は次のエラーを取得する:

An error occurred in a `before(:suite)` hook. 
Failure/Error: DatabaseCleaner.clean_with(:truncation) 

Sequel::DatabaseError: 
    Mysql2::Error: Cannot truncate a table referenced in a foreign key constraint (`account_users`.`memberships`, CONSTRAINT `memberships_ibfk_2` FOREIGN KEY (`account_id`) REFERENCES `account_users`.`accounts` (`id`)) 

マイDatabaseCleanerのセットアップは次のとおりです。

config.before(:suite) do 
    DatabaseCleaner.strategy = :transaction 
    DatabaseCleaner.clean_with(:truncation) 
    end 

DatabaseCleanerがために設定解除する必要がありますActiveRecordと同様、切り詰め前にキー制約を適用しても問題ありません。

私の質問は:これはDatabaseCleaner-Sequelのバグですか、それともSequel many_through_manyプラグインの使用方法ですか?

答えて

1

これは公開issue with DatabaseCleanerです。この問題を回避するには、参照キー制約を後で切り捨てて再度有効にする前に、それらを無効にすることです。

のMySQLで、それはこのようになります:

DB.run('SET FOREIGN_KEY_CHECKS=0;') 
DatabaseCleaner.clean_with(:truncation) 
DB.run('SET FOREIGN_KEY_CHECKS=1;') 
関連する問題