Chizpon's avatar

Drop foreign

Hi!

I'm working on the tests on a project with sqlite as default, but someone in the team added a dropforeign and it broke everything. There's an answer that suggests to use something like

(DB::getDriverName() !== 'sqlite') ?: $table->dropForeign(['xxx']);

But getdrivername is non existant in the facade, and the closest i found is getdefaultconnection

(DB::getDefaultConnection() !== 'sqlite') ?: $table->dropForeign(['xxx']);

dd'ing this ( DB::getDefaultConnection() !== 'sqlite') ) returns false, i assume it's not getting the db i'm using on the time that i'm testing, so the error persists:

SQLite doesn't support dropping foreign keys (you would need to re-create the table).

Thanks in advance for the help.

0 likes
5 replies
BryanK's avatar

Try this instead:

config('database.default') !== 'sqlite' ?: $table->dropForeign(['xxx']);
BryanK's avatar

Is your testing environment set correctly?

phpunit.xml

<php>
        <server name="APP_ENV" value="testing"/>
        <server name="BCRYPT_ROUNDS" value="4"/>
        <server name="CACHE_DRIVER" value="array"/>
        <server name="MAIL_DRIVER" value="array"/>
        <server name="QUEUE_CONNECTION" value="sync"/>
        <server name="SESSION_DRIVER" value="array"/>
        <server name="DB_CONNECTION" value="sqlite"/> // is this set?
        <server name="DB_DATABASE" value=":memory:"/> // and this?
</php>
BryanK's avatar
BryanK
Best Answer
Level 16

Sorry, I copy pasted your code and forgot to remove the ! from !==

config('database.default') == 'sqlite' ?: $table->dropForeign(['xxx']);
1 like
Chizpon's avatar

Yep, i'm double checking everything... i'm probably missing something stupid

Please or to participate in this conversation.