Damaon
Damaon

Reputation: 612

Rails: where to put PRAGMA FOREIGNS KEYS = ON for SQLite

Where should I put "PRAGMA FOREIGNS KEYS = ON;" in rails 4.0?

I know it's connection setting so I have to make rails put it anytime it deletes something, but I don't know where to put it. I tried in database.yml in options and it's not working. Can't google where to put it. I want it to make cascade delete work.

Upvotes: 1

Views: 253

Answers (1)

SemperFi
SemperFi

Reputation: 2378

the syntax is like this:

drop table if exists db_name.table_name

where the if exists and db_name. parts are optional. There is no CASCADE in there so SQLite simply doesn't support CASCADE when using DROP TABLE and there's nothing you can do to add it (unless, of course, you want to hack the SQLite C source and add it yourself). You have to accept certain limitations when using SQLite, this is one of them.

If you want to use one migration for both SQLite and MySQL then you'll have to check which database is being used and execute the appropriate SQL or find something that works everywhere (i.e. perform the CASCADE by hand). The easy way to check which database you're using that I can think of right now would be:

    case connection
    when ActiveRecord::ConnectionAdapters::PostgreSQLAdapter
        # PostgreSQL things go here
    when ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter
        # MySQL things go here
    when ActiveRecord::ConnectionAdapters::SQLiteAdapter
        # SQLite things go here

...
end

Upvotes: 1

Related Questions