cashapp / sqldelight

SQLDelight - Generates typesafe Kotlin APIs from SQL
https://cashapp.github.io/sqldelight/
Apache License 2.0
6.03k stars 503 forks source link

Unable to disable foreign key checks #4931

Open kirillzh opened 6 months ago

kirillzh commented 6 months ago

SQLDelight Version

2.0.0

SQLDelight Dialect

Sqlite (jvm)

Describe the Bug

Attempting to disable foregin key checks throws error:

driver.execute(null, "SET FOREIGN_KEY_CHECKS = 0;", 0)

Stacktrace

org.sqlite.SQLiteException: [SQLITE_ERROR] SQL error or missing database (near "SET": syntax error)
    at org.sqlite.core.DB.newSQLException(DB.java:1179)
    at org.sqlite.core.DB.newSQLException(DB.java:1190)
    at org.sqlite.core.DB.throwex(DB.java:1150)
    at org.sqlite.core.NativeDB.prepare_utf8(Native Method)
    at org.sqlite.core.NativeDB.prepare(NativeDB.java:126)
    at org.sqlite.core.DB.prepare(DB.java:264)
    at org.sqlite.core.CorePreparedStatement.<init>(CorePreparedStatement.java:46)
    at org.sqlite.jdbc3.JDBC3PreparedStatement.<init>(JDBC3PreparedStatement.java:32)
    at org.sqlite.jdbc4.JDBC4PreparedStatement.<init>(JDBC4PreparedStatement.java:25)
    at org.sqlite.jdbc4.JDBC4Connection.prepareStatement(JDBC4Connection.java:34)
    at org.sqlite.jdbc3.JDBC3Connection.prepareStatement(JDBC3Connection.java:226)
    at org.sqlite.jdbc3.JDBC3Connection.prepareStatement(JDBC3Connection.java:206)
    at app.cash.sqldelight.driver.jdbc.JdbcDriver.execute(JdbcDriver.kt:130)
kirillzh commented 6 months ago

Also attempted to turn off foreign key checks like so PRAGMA foreign_keys = OFF. The query does pass but doesn't seem to actually disable the check, deleting tables that run into foreign key constraint fails:

org.sqlite.SQLiteException: [SQLITE_CONSTRAINT_FOREIGNKEY] A foreign key constraint failed (FOREIGN KEY constraint failed)
Knoxvillekm commented 2 months ago

When using driver from the documentation

JdbcSqliteDriver(
  url = "...", 
  properties = Properties().apply { put("foreign_keys", "true") }
)

Having same problem with disabling foreign_keys during migrations. Unable to alter table with FK (Copy, delete, create and insert data from temp)