无法对SQLite数据库运行查询

pinkon5k  于 2022-11-14  发布在  SQLite
关注(0)|答案(1)|浏览(243)

我正在尝试对一个SQLite数据库运行迁移脚本。以下是我在尝试迁移查询时收到的错误:

node:internal/process/promises:288
            triggerUncaughtException(err, true /* fromPromise */);
            ^

[Error: SQLITE_ERROR: near "categoryId": syntax error] {
  errno: 1,
  code: 'SQLITE_ERROR'
}

我正在运行用于创建表的SQL查询

CREATE TABLE IF NOT EXISTS "Category" (
    id INTEGER PRIMARY KEY NOT NULL,
    name VARCHAR NOT NULL ); 

CREATE TABLE IF NOT EXISTS "Location"(
        id INTEGER PRIMARY KEY NOT NULL,
        name VARCHAR NOT NULL ); 

CREATE TABLE IF NOT EXISTS "Job" (
        id INTEGER PRIMARY KEY NOT NULL,
        createdAt datetime NOT NULL default current_time,
        title VARCHAR NOT NULL,
        description VARCHAR NOT NULL,
        locationId INTEGER NOT NULL,
        FOREIGN KEY(locationId) REFERENCES Location(id),
        categoryId INTEGER NOT NULL,
        FOREIGN KEY(categoryId) REFERENCES Category(id) );
hujrc8aj

hujrc8aj1#

在定义约束之前,您需要定义所有列,因此将脚本中倒数第二行和倒数第三行互换:

CREATE TABLE IF NOT EXISTS "Job" (
        id INTEGER PRIMARY KEY NOT NULL,
        createdAt datetime NOT NULL default current_time,
        title VARCHAR NOT NULL,
        description VARCHAR NOT NULL,
        locationId INTEGER NOT NULL,
        categoryId INTEGER NOT NULL,
        FOREIGN KEY(locationId) REFERENCES Location(id),
        FOREIGN KEY(categoryId) REFERENCES Category(id) );

相关问题