SQLite3 “forgets” to use foreign keys

前端 未结 3 1321
我在风中等你
我在风中等你 2021-01-03 23:49

I\'m using Ruby with SQLite3 and my attempts to use foreign keys in Sqlite3 were unfortunately not successful. According to sqlite3 --version, version 3.7.13 is

相关标签:
3条回答
  • 2021-01-04 00:03

    Put this at the top of the file that executes the SQL commands and it will enable foreign keys on runtime.

    db = SQLite3::Database.new("database.db")
    db.execute("PRAGMA foreign_keys = ON")
    
    0 讨论(0)
  • 2021-01-04 00:19

    I think I can answer my own question: The documentation says: Foreign key constraints are disabled by default (for backwards compatibility), so must be enabled for each database connection separately. Annoying, but it's finally working now.

    0 讨论(0)
  • 2021-01-04 00:20

    One way of permanently turning on foreign_keys by default is to inject the following line into ~/.sqliterc:

    PRAGMA foreign_keys = ON;
    

    Please note that it will affect all your databases...

    0 讨论(0)
提交回复
热议问题