-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tests for sqilte: enable FOREIGN_KEYS inside OpenTestConnection #6641
Conversation
@jinzhu it seems like tests are failing unrelated to this fix. Can you please re-run them? |
Are you sure this works? |
Nice notice. Agreed. |
Tbh, I don't know of a good way to solve this across drivers, which is why I think go-gorm/sqlite#161 is a bad idea. |
One option is for the GORM drivers to enable this on their own regardless of the DSN, like: |
I am still not sure why we changed this at all, gorm/sqlite driver is using mattn's CGo driver by default, for one of the gorm functionalities(TranslateError) we wrote a test and we want to run the tests based on the default driver to get it green, not sure why we should really think to make changes in test part if we use the mattn CGo driver by default in gorm/sqlite. Note: This test was written for TranslateError functionality and for this functionality we don't depend on any driver(we use JSON decoding), however, to just test and verify the functionality we go for mattn CGo driver. CC: @a631807682 |
If foreign keys should be on by default, why doesn't each GORM driver ( |
-> foreign keys should be on by default This is enabled only in testing, and it is for specific test cases that are relaying on github.com/go-gorm/sqlite -> github.com/mattn/go-sqlite3. It is nothing to do with Gorm functionality, it is used only in test files. |
As far as I know, foreign_keys only applies to the limitations of SQLite database when using a single connection. We use it to test the association between SQLite tables. This does not mean that it is necessary (even we will not use it in most cases). It can be decided by the user. |
What did this pull request do?
As of now, the Foreign Key constraint is OFF by default in
sqlite3
.In GORM
tests
package the constraint is enabled by running following code:gorm/tests/tests_test.go
Lines 46 to 48 in 6bef318
for the global
DB
instance.The problem is that this code is executed once inside
init
, thus leaving other calls toOpenTestConnection
not executing the same logic.The code should be moved inside
OpenTestConnection
, since some tests do not use the globalDB
instance, preferring to callOpenTestConnection
for their own purpose.The issue was solved the wrong way in c10f807 by changing the connection string to
"gorm.db?_foreign_keys=on"
, which is a syntax, specific to mattn's CGo driver, and caused regression in CGo-free driver.This PR addresses the issue in a unified way, compatible with all sqlite drivers for GORM.
User Case Description