jeudi 24 décembre 2015

Foreign Key constraint issues using sequel and database cleaner

I am running into issues using database cleaner with sequel and sqlite foreign key constraints. Specifically, I am using the :truncation strategy with Capybara integration tests.

For the given sample schema:

CREATE TABLE users(id INTEGER PRIMARY KEY, name TEXT);
CREATE TABLE events(id INTEGER PRIMARY KEY, title TEXT);

CREATE TABLE events_users(
  user_id INTEGER,
  event_id INTEGER,

  FOREIGN KEY(user_id) REFERENCES users(id),
  FOREIGN KEY(event_id) REFERENCES events(id)
);

And sequel models:

class User < Sequel::Model
  one_to_many :invitations_received, :class => :Invitation, :key => :inviter_id
  one_to_many :invitations_given, :class => :Invitation, :key => :invitee_id
  many_to_many :events
end

class Event < Sequel::Model
  many_to_many :users
  one_to_many :invitations
end

Running the following:

# normally this would be run in
# an rspec before(:each) for my :feature specs
DatabaseCleaner.start
DatabaseCleaner.strategy = :truncation

bob = User.create(name: "bob")
sally = User.create(name: "sally")
event = Event.create(title: "Everyone's invited")
event.users << [bob, sally]

DatabaseCleaner.clean

Results in an error

SQLite3::ConstraintException: FOREIGN KEY constraint failed (Sequel::ForeignKeyConstraintViolation)

I can get around this by altering my before statement to disable the foreign_keys PRAGMA:

DB.foreign_keys = false
DatabaseCleaner.start
DatabaseCleaner.strategy = :truncation

(or not using FOREIGN KEY in my tables), but that seems wrong since I want the benefits that foreign keys constraints — or at least I think I do ;).

Is this a fundamental misunderstanding of how to use foreign key constraints, or is there a better way to do this?

Aucun commentaire:

Enregistrer un commentaire