- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Closed
Description
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.
aleksbelic and Rep0seNix
Activity
[-]Foreign Key Delete Cascade doen[/-][+]Foreign Key Delete Cascade dosen't work[/+]santosorie commentedon Feb 22, 2018
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 commentedon Feb 22, 2018
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 commentedon Mar 11, 2018
Thank you for replay.
it works for me. Thank you again :)
de3z1e commentedon Mar 12, 2018
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.
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.