So I am building a CRUD blog with Node.js It is using the following NPM packages: (body-parser, method-override, express, EJS, Sqlite3) it is on Mac OS X Yosemite if that's at all relevant. For the full code you may reference: https://github.com/zargold/Blog. I have gotten everything to work for blogging the posts (It can Create (new post) Read (view all posts) Update (edit the posts) and Delete posts.
However, for comments although everything else works perfectly, it refuses to give me the Updated time_stamp (it basically remains null even after being updated several times. The rest of the web App works perfectly. The comments can also be written, viewed, editted, and deleted. It also uses the exact same trigger (slightly changed) for the comments table (within the same database).
I am about 95% sure that the problem is with the trigger or SQLite 3 rather than the node.js Express Server or even the SQLite3 npm module. I may have to delete the whole db and start it all up again... which would be a pain.
Here is what I know...
[ { id: 18,
tag: 'you need to get over yourself',
body: 'Why do I always compulsively read everything you post! My worst life choice was reading this!',
author: '(not) Jenna',
postID: 4,
updated_at: null } ]
4
[ { id: 18,
tag: 'you need to get over yourself',
body: 'Why do I always compulsively read everything you post! My worst life choice was reading this!',
author: '(not) Jenna',
postID: 4,
updated_at: null } ]
That is the console.log for the comments which I have going from where you edit comments. As you can see "updated_at is null..." and whoa is me.
The similar output for an actual sample post: (it may be because of the amazing content that of the post that it decided to work there but not for the comment).
{ id: 2,
title: '10 things I love about you!',
body: 'So no one told you life was gonna be this way\r\nYour job\'s a joke, you\'re broke, your love life\'s D.O.A.\r\nIt\'s like you\'re always stuck in second gear\r\nWhen it hasn\'t been your day, your week, your month, or even your year, but',
imageUrl: 'http://ecx.images-amazon.com/images/I/71wKbAQU2AL._SL1000_.jpg',
updated_at: '2015-04-13 11:02:50' }
It is no surprise that my updated_at thing which I try to print in my EJS file doesn't work because the actual content is null.
However that could still theoretically happen if my trigger was working fine but update is not working. Here is the proof that it actually doesn't work despite having identical SYNTAX to the SYNTAX in the similar updated_at for my other table.
sqlite> INSERT INTO comments (tag, body, author, postID) VALUES ("amazing post thanks","so creative so lush in imagination, so obviously written by the commentator", "I swear I'm not the author of the post", "1");
INSERT INTO comments (tag, body, author, postID) VALUES ("amazing post thanks","so creative so lush in imagination, so obviously written by the commentator", "I swear I'm not the author of the post", "1");
sqlite> SELECT * FROM comments WHERE postID=1;
id tag body author postID updated_at
---------- ---------- --------------------------------------------------------------------------------------------- ---------- ---------- ----------
1 The bomb! This article was the bomb! Thank you so much for sharing. Also this is such a cool website... yellow WHO KNEW? Twitch 1
2 amazing po so creative so lush in imagination, so obviously written by the commentator I swear I' 1
The actual sqlite3 database shows that it is null even if I updated it with code within SQlite3.
Now for the actual triggers:
-- DROP TABLE IF EXISTS posts; CREATE TABLE posts ( id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, body TEXT, imageUrl TEXT, updated_at REAL);
-- CREATE TRIGGER timestamp_update BEFORE UPDATE ON posts BEGIN UPDATE posts SET updated_at = CURRENT_TIMESTAMP WHERE id = new.id; END;
DROP TABLE IF EXISTS comments; CREATE TABLE comments ( id INTEGER PRIMARY KEY AUTOINCREMENT, tag TEXT, body TEXT, author TEXT, postID INTEGER, updated_at REAL);
DELETE TRIGGER IF EXISTS timestamp_update; CREATE TRIGGER timestamp_update BEFORE UPDATE ON comments BEGIN UPDATE comments SET updated_at = CURRENT_TIMESTAMP WHERE id = new.id; END;
That was within my schema.sql before I did Blog/db [880]$ sqlite3 blog.db < schema.sql that is taken right out of my bash... Now as you can see the syntax is identical... Ok so 1 problem may be that when I did it the first time it didn't seem to work for some reason then I did it again... that time it told me trigger is already made... So I don't know that's why I added the DELETE trigger thing if exists (but that doesn't seem to work) what I actually put was the version before the DELETE TRIGGER if exists... Anyway long and short is there a way to delete previous trigger put in new one without deleting the whole db or whole table?
Aucun commentaire:
Enregistrer un commentaire