Laravel 5.1: Enable SQLite foreign key constraints

后端 未结 4 685
一个人的身影
一个人的身影 2020-12-31 08:10

In SQLite, foreign key constraints are disabled by default.

What\'s the best way to configure Laravel 5.1\'s SQLite database connection to enable foreign key constra

4条回答
  •  我在风中等你
    2020-12-31 08:39

    Since I only want to use this in my tests, but in all tests, I ended up with a simple implementation in the Tests\TestCase class like this:

     abstract class TestCase extends BaseTestCase
     {
            use CreatesApplication;
    
            protected function setUp()
            {
                parent::setUp();
    
                $this->enableForeignKeys();
            }
    
            /**
             * Enables foreign keys.
             *
             * @return void
             */
            public function enableForeignKeys()
            {
                $db = app()->make('db');
                $db->getSchemaBuilder()->enableForeignKeyConstraints();
            }
    }
    

    This works like a charm :-)

提交回复
热议问题