Skip to content

Commit 0813ce8

Browse files
committed
Add integer primary key columns to db.
1 parent 4057d9a commit 0813ce8

File tree

2 files changed

+7
-4
lines changed

2 files changed

+7
-4
lines changed

db.js

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ var db = new sqlite3.Database('var/db/todos.db');
99
db.serialize(function() {
1010
// create the database schema for the todos app
1111
db.run("CREATE TABLE IF NOT EXISTS users ( \
12+
id INTEGER PRIMARY KEY, \
1213
username TEXT UNIQUE, \
1314
hashed_password BLOB, \
1415
salt BLOB, \
@@ -18,13 +19,15 @@ db.serialize(function() {
1819
)");
1920

2021
db.run("CREATE TABLE IF NOT EXISTS federated_credentials ( \
22+
id INTEGER PRIMARY KEY, \
2123
user_id INTEGER NOT NULL, \
2224
provider TEXT NOT NULL, \
2325
subject TEXT NOT NULL, \
2426
PRIMARY KEY (provider, subject) \
2527
)");
2628

2729
db.run("CREATE TABLE IF NOT EXISTS todos ( \
30+
id INTEGER PRIMARY KEY, \
2831
owner_id INTEGER NOT NULL, \
2932
title TEXT NOT NULL, \
3033
completed INTEGER \

routes/index.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ var express = require('express');
22
var db = require('../db');
33

44
function fetchTodos(req, res, next) {
5-
db.all('SELECT rowid AS id, * FROM todos WHERE owner_id = ?', [
5+
db.all('SELECT * FROM todos WHERE owner_id = ?', [
66
req.user.id
77
], function(err, rows) {
88
if (err) { return next(err); }
@@ -67,15 +67,15 @@ router.post('/:id(\\d+)', function(req, res, next) {
6767
next();
6868
}, function(req, res, next) {
6969
if (req.body.title !== '') { return next(); }
70-
db.run('DELETE FROM todos WHERE rowid = ? AND owner_id = ?', [
70+
db.run('DELETE FROM todos WHERE id = ? AND owner_id = ?', [
7171
req.params.id,
7272
req.user.id
7373
], function(err) {
7474
if (err) { return next(err); }
7575
return res.redirect('/' + (req.body.filter || ''));
7676
});
7777
}, function(req, res, next) {
78-
db.run('UPDATE todos SET title = ?, completed = ? WHERE rowid = ? AND owner_id = ?', [
78+
db.run('UPDATE todos SET title = ?, completed = ? WHERE id = ? AND owner_id = ?', [
7979
req.body.title,
8080
req.body.completed !== undefined ? 1 : null,
8181
req.params.id,
@@ -87,7 +87,7 @@ router.post('/:id(\\d+)', function(req, res, next) {
8787
});
8888

8989
router.post('/:id(\\d+)/delete', function(req, res, next) {
90-
db.run('DELETE FROM todos WHERE rowid = ? AND owner_id = ?', [
90+
db.run('DELETE FROM todos WHERE id = ? AND owner_id = ?', [
9191
req.params.id,
9292
req.user.id
9393
], function(err) {

0 commit comments

Comments
 (0)