Skip to content

Foreign Key Delete Cascade dosen't work #757

@rangshu

Description

@rangshu
            try db!.run(trip.create(ifNotExists: true) { table in
                table.column(trip_Id, primaryKey: .autoincrement)
                table.column(trip_Title)
                table.column(trip_Start_Date)
                table.column(trip_End_Date)
                table.column(trip_Reg_Date)
            })
            try db!.run(trip.createIndex(trip_Id, unique: true, ifNotExists: true))
            try db!.run(trip.createIndex(trip_Start_Date, unique: false, ifNotExists: true))
            
            try db!.run(dailySchedule.create(ifNotExists: true) { table in
                table.column(dailySchedule_Id, primaryKey: .autoincrement)
                table.column(dailySchedule_Trip_Id)
                table.column(dailySchedule_Date)
                table.column(dailySchedule_Reg_Date)
                table.foreignKey(dailySchedule_Trip_Id, references: trip, trip_Id, delete: .cascade)
            })
            try db!.run(dailySchedule.createIndex(dailySchedule_Id, unique: true, ifNotExists: true))
            
            try db!.run(dailyScheduleDetail.create(ifNotExists: true) { table in
                table.column(dailyScheduleDetail_Id, primaryKey: .autoincrement)
                table.column(dailyScheduleDetail_DailySchedule_Id)
                table.column(dailyScheduleDetail_Time)
                table.column(dailyScheduleDetail_Title)
                table.column(dailyScheduleDetail_Description)
                table.column(dailyScheduleDetail_Date)
                table.column(dailyScheduleDetail_Reg_Date)
                table.foreignKey(dailyScheduleDetail_DailySchedule_Id, references: dailySchedule, dailySchedule_Id, delete: .cascade)
            })
            try db!.run(dailyScheduleDetail.createIndex(dailyScheduleDetail_Id, unique: true, ifNotExists: true))
            try db!.run(dailyScheduleDetail.createIndex(dailyScheduleDetail_Time, unique: false, ifNotExists: true))

Here is my code.
when i delete "trip.id"
i want to delete all relative row.

but, i think, cascade doesn't work.

Activity

changed the title [-]Foreign Key Delete Cascade doen[/-] [+]Foreign Key Delete Cascade dosen't work[/+] on Nov 14, 2017
santosorie

santosorie commented on Feb 22, 2018

@santosorie

i also find this issue..
my problem is exactly the same..
and i am still looking for the solution..

do you have solved this?

santosorie

santosorie commented on Feb 22, 2018

@santosorie

hi i just solved this, but i dont know this is the right way.. but since it worked for me so i will share it here

to do this you must change one line of code in pods file
go to pods -> SQLite.swift -> statement.swift
in file statement.swift -> init
put this code
sqlite3_exec(connection.handle, "PRAGMA foreign_keys = on", nil, nil, nil)
just before
try connection.check(sqlite3_prepare_v2(connection.handle, SQL, -1, &handle, nil))

after i read bunch of Q & A, there were option db.foreignKeys = true but i cant found it,
maybe if you don't want to change the pod file, you should find this

rangshu

rangshu commented on Mar 11, 2018

@rangshu
Author

Thank you for replay.

it works for me. Thank you again :)

de3z1e

de3z1e commented on Mar 12, 2018

@de3z1e

Unfortunately, the proposed solution requires you to change the source code of the SQLite.swift package, which may create problems later on if you update to a new version and forget to add back that line of code. After searching the depths of the Internet and not finding a suitable fix, this is what ended up working for me.

Just add the following line after you open a database connection to execute SQL statements directly using the handy SQLite.swift API db.execute("...") where db is a Connection object.

do {
    let path = "/Users/User/Database/db.sqlite"
    let db = try Connection(path) 
    try db.execute("PRAGMA foreign_keys = ON;") // turns on foreign keys support for the db connection
} catch {
    // handle error
}

Even after the library is compiled with foreign key constraints enabled, it must still be enabled by the application at runtime using the PRAGMA foreign_keys command. Here is a reference of the SQLite docs at http://www.sqlite.org/foreignkeys.html#fk_enable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @rangshu@de3z1e@santosorie

        Issue actions

          Foreign Key Delete Cascade dosen't work · Issue #757 · stephencelis/SQLite.swift