8

I'm using phpunit with Symfony2.

I decided to use sqlite for my tests.

The issue I'm having is that the foreign keys constraints are ignored.

I know I have to execute the following query in order to use foreign keys : PRAGMA foreign_keys = ON).

My question is : is there a way to always use foreign keys when creating the database schema with sqlite ?

Thanks !

1 Answer 1

3

Unfortunately it is impossible. Accordingly to SQLite documentation:

Assuming the library is compiled with foreign key constraints enabled, it must still be enabled by the application at runtime, using the PRAGMA foreign_keys command.

I would suggest to create your own test case class and use setUp() method to enable foreign keys.

class SQLiteTestCase extends \PHPUnit_Framework_TestCase
{
  protected function setUp()
  {
    parent::setUp();
    // Here add your code to enable foreign keys
  }
}

class MyTest extends SQLiteTestCase
{
  protected function setUp()
  {
    // Setup your test data-set here
    parent::setUp();
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.