我正在以下 Sequel 模型上使用 DatabaseCleaner 运行 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 插件有关?