我在 Symfony2 中使用 phpunit。
我决定使用 sqlite 进行测试。
我遇到的问题是外键约束被忽略了。
我知道我必须执行以下查询才能使用外键:PRAGMA foreign_keys = ON
)。
我的问题是:有没有办法在使用 sqlite 创建数据库模式时始终使用外键?
谢谢!
最佳答案
不幸的是,这是不可能的。根据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.
我建议创建您自己的测试用例类并使用 setUp() 方法启用外键。
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();
}
}
关于sqlite - Symfony2 : Unit testing with sqlite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26363623/