mercredi 18 février 2015

Android create trigger similar to On Delete Set Null

I've an Android application that uses sqlite database. I'm trying to create a trigger that when a post is deleted it sets the comment "post_id" to null. Like a "On Delete Set Null" on a foreign key. I've done the following:



CREATE TABLE Post (id TEXT PRIMARY KEY,dirty_row INTEGER,updated_at INTEGER,created_at INTEGER,calendarDay TEXT,calendarTime TEXT,date TEXT,text TEXT,title TEXT);
CREATE TABLE PostComment (id TEXT PRIMARY KEY,dirty_row INTEGER,updated_at INTEGER,created_at INTEGER,postId TEXT,text TEXT,score INTEGER);
CREATE TRIGGER fkd_Post_PostComment BEFORE DELETE ON Post FOR EACH ROW BEGIN UPDATE PostComment SET postId=NULL WHERE postId = OLD.id END;


What I have missed?


Note: I know that this can be achieved by a foregin key and On Delete Set Null, but I don't want to add restriction that foreign keys add, only the on delete behaviour.


Thanks


Aucun commentaire:

Enregistrer un commentaire