最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

node.js - SQLite FOREIGN KEY constraint not being triggered from NodeJSJavaScript - Stack Overflow

programmeradmin3浏览0评论

I am trying to create a POST route as shown below. My Timesheet table has a FOREIGN KEY constraint set on it referencing the Employee table. When I try and insert a timesheet from an employee that doesn't exist using DB Browser I get the correct error triggered by the constraint. However, when I run the code below, it ignores this constraint and inserts the record. Have I set this up incorrectly or is this not a valid constraint under this scenario?

timesheetRouter.post("/", getParams, (req, res, next) => {
    const tsData = req.body.timesheet;
    console.log("ID", req.empID);
    db.run(`insert into Timesheet (hours, rate, date, employee_id) 
        values ($hours, $rate, $date, $emp);`, {
        $hours: tsData.hours,
        $rate: tsData.rate,
        $date: tsData.date,
        $emp: req.empID
    }, function(err) {
        if (err) {
            return res.status(404).send(err);
        } else {
            db.get("select * from Timesheet where id = $id", {$id: this.lastID},
                (err, row) => {
                    return res.status(201).send({timesheet: row});
                });
        }
    });
});

Table creation code:

db.serialize(() => {
    db.run("DROP TABLE IF EXISTS Timesheet", err => {
        if (err) {
            throw new Error(err);
        }
    });
    db.run(`create table Timesheet (id integer PRIMARY KEY,
        hours integer NOT NULL,
        rate integer NOT NULL,
        date integer NOT NULL,
        employee_id integer NOT NULL, FOREIGN KEY (employee_id) REFERENCES Employee (id))`,
    err => {
        if (err) {
            throw new Error(err);
        }
    });
});

Note: getParams is a function to determine the value for empID.

I am trying to create a POST route as shown below. My Timesheet table has a FOREIGN KEY constraint set on it referencing the Employee table. When I try and insert a timesheet from an employee that doesn't exist using DB Browser I get the correct error triggered by the constraint. However, when I run the code below, it ignores this constraint and inserts the record. Have I set this up incorrectly or is this not a valid constraint under this scenario?

timesheetRouter.post("/", getParams, (req, res, next) => {
    const tsData = req.body.timesheet;
    console.log("ID", req.empID);
    db.run(`insert into Timesheet (hours, rate, date, employee_id) 
        values ($hours, $rate, $date, $emp);`, {
        $hours: tsData.hours,
        $rate: tsData.rate,
        $date: tsData.date,
        $emp: req.empID
    }, function(err) {
        if (err) {
            return res.status(404).send(err);
        } else {
            db.get("select * from Timesheet where id = $id", {$id: this.lastID},
                (err, row) => {
                    return res.status(201).send({timesheet: row});
                });
        }
    });
});

Table creation code:

db.serialize(() => {
    db.run("DROP TABLE IF EXISTS Timesheet", err => {
        if (err) {
            throw new Error(err);
        }
    });
    db.run(`create table Timesheet (id integer PRIMARY KEY,
        hours integer NOT NULL,
        rate integer NOT NULL,
        date integer NOT NULL,
        employee_id integer NOT NULL, FOREIGN KEY (employee_id) REFERENCES Employee (id))`,
    err => {
        if (err) {
            throw new Error(err);
        }
    });
});

Note: getParams is a function to determine the value for empID.

Share Improve this question asked Oct 31, 2018 at 13:42 nathanjwnathanjw 1,0622 gold badges15 silver badges27 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 18

Currently, in SQLite version 3.25.2 foreign key constraints are disabled by default. You need to turn them on by

sqlite> PRAGMA foreign_keys = ON;

Or, in your case

db.get("PRAGMA foreign_keys = ON")

I assume.

It works on a per-connection basis, DB Browser probably automatically sets it on start up, that's why it triggers an error in that case.

发布评论

评论列表(0)

  1. 暂无评论