Laravel 5.1: Enable SQLite foreign key constraints

后端 未结 4 684
一个人的身影
一个人的身影 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:37

    You could also activate foreign keys on a per test (file) basis, when the tests actually depend on tables with foreign keys.

    Here's a trait: (e.g. tests/ForeignKeys.php)

    make('db');
            $db->getSchemaBuilder()->enableForeignKeyConstraints();
        }
    }
    

    don't forget to run the method somewhere in your test set-up chain. I added mine as an override to my TestCase: (tests/TestCase.php)

    enableForeignKeys();
            }
        }
    
        ...
    

    after that you can add it to your tests like so:

提交回复
热议问题