ORM - rejection error: column "id" does not exist
ORM - rejection error: column "id" does not exist
我在 ORM 的 Node.js 后端使用 PostgreSQL 和 objection.js。我只有两个具有一对一关系的简单 table。我无法将新记录插入 table.
我有一个包含员工和薪水的简单数据库模式 table:
CREATE TABLE employee (
employee_id SERIAL PRIMARY KEY,
employee_name VARCHAR
);
INSERT INTO employee (employee_name) VALUES ('james');
CREATE TABLE salary (
salary_id SERIAL PRIMARY KEY,
employee_id SERIAL UNIQUE,
FOREIGN KEY (employee_id) REFERENCES employee (employee_id),
amount integer
);
如果我想通过objection.js创建新的工资记录:
Salary.query()
.insert({ employee_id: 1, amount: 10 })
.then((data) => {
console.log(data);
})
.catch((err) => {
throw err;
});
我收到错误:
Unhandled rejection error: column "id" does not exist
at Connection.parseE (./node_modules/pg/lib/connection.js:546:11)
at Connection.parseMessage (./node_modules/pg/lib/connection.js:371:19)
at TLSSocket.<anonymous> (./node_modules/pg/lib/connection.js:114:22)
at emitOne (events.js:115:13)
at TLSSocket.emit (events.js:210:7)
at addChunk (_stream_readable.js:252:12)
at readableAddChunk (_stream_readable.js:239:11)
at TLSSocket.Readable.push (_stream_readable.js:197:10)
at TLSWrap.onread (net.js:589:20)
testSalary.js
const { Model, snakeCaseMappers } = require('objection');
class Salary extends Model {
static get tableName() {
return 'salary';
}
static get columnNameMappers() {
return snakeCaseMappers();
}
static get jsonSchema() {
return {
type: 'object',
properties: {
salary_id: { type: 'integer' },
employee_id: { type: 'integer' },
amount: { type: 'integer' },
},
};
}
static get relationMappings() {
return {
employee: {
relation: Model.BelongsToOneRelation,
modelClass: `${__dirname}/testEmployee`,
join: {
from: 'salary.employee_id',
to: 'employee.employee_id',
},
},
};
}
}
module.exports = Salary;
testEmployee.js
const { Model, snakeCaseMappers } = require('objection');
class Employee extends Model {
static get tableName() {
return 'employee';
}
static get columnNameMappers() {
return snakeCaseMappers();
}
static get jsonSchema() {
return {
type: 'object',
properties: {
employee_id: { type: 'integer' },
employee_name: { type: 'string' },
},
};
}
static get relationMappings() {
return {
salary: {
relation: Model.HasManyRelation,
modelClass: `${__dirname}/testSalary`,
join: {
from: 'employee.employee_id',
to: 'salary.employee_id',
},
},
};
}
}
module.exports = Employee;
我的猜测是这与引擎盖下对 Knex 的依赖有关。在 the docs 中,有一个代码片段建议通过 Knex 迁移创建表。也许这样做会解决您的问题,因为 Knex 可能会创建一个反对意见期望存在的 id 列。
需要通过在 Model
类.
上定义 idColumn
来告知反对意见 id 列是什么
class Salary extends Model {
static get tableName() {
return 'salary';
}
static get idColumn() {
return 'salary_id';
}
// etc
}
使用迁移来创建表将通过创建表并将其 id 列命名为 id
来完成这项工作,但是如果您定义 idColumn
,则可以在没有迁移的情况下使用异议。 =15=]
我在 ORM 的 Node.js 后端使用 PostgreSQL 和 objection.js。我只有两个具有一对一关系的简单 table。我无法将新记录插入 table.
我有一个包含员工和薪水的简单数据库模式 table:
CREATE TABLE employee (
employee_id SERIAL PRIMARY KEY,
employee_name VARCHAR
);
INSERT INTO employee (employee_name) VALUES ('james');
CREATE TABLE salary (
salary_id SERIAL PRIMARY KEY,
employee_id SERIAL UNIQUE,
FOREIGN KEY (employee_id) REFERENCES employee (employee_id),
amount integer
);
如果我想通过objection.js创建新的工资记录:
Salary.query()
.insert({ employee_id: 1, amount: 10 })
.then((data) => {
console.log(data);
})
.catch((err) => {
throw err;
});
我收到错误:
Unhandled rejection error: column "id" does not exist
at Connection.parseE (./node_modules/pg/lib/connection.js:546:11)
at Connection.parseMessage (./node_modules/pg/lib/connection.js:371:19)
at TLSSocket.<anonymous> (./node_modules/pg/lib/connection.js:114:22)
at emitOne (events.js:115:13)
at TLSSocket.emit (events.js:210:7)
at addChunk (_stream_readable.js:252:12)
at readableAddChunk (_stream_readable.js:239:11)
at TLSSocket.Readable.push (_stream_readable.js:197:10)
at TLSWrap.onread (net.js:589:20)
testSalary.js
const { Model, snakeCaseMappers } = require('objection');
class Salary extends Model {
static get tableName() {
return 'salary';
}
static get columnNameMappers() {
return snakeCaseMappers();
}
static get jsonSchema() {
return {
type: 'object',
properties: {
salary_id: { type: 'integer' },
employee_id: { type: 'integer' },
amount: { type: 'integer' },
},
};
}
static get relationMappings() {
return {
employee: {
relation: Model.BelongsToOneRelation,
modelClass: `${__dirname}/testEmployee`,
join: {
from: 'salary.employee_id',
to: 'employee.employee_id',
},
},
};
}
}
module.exports = Salary;
testEmployee.js
const { Model, snakeCaseMappers } = require('objection');
class Employee extends Model {
static get tableName() {
return 'employee';
}
static get columnNameMappers() {
return snakeCaseMappers();
}
static get jsonSchema() {
return {
type: 'object',
properties: {
employee_id: { type: 'integer' },
employee_name: { type: 'string' },
},
};
}
static get relationMappings() {
return {
salary: {
relation: Model.HasManyRelation,
modelClass: `${__dirname}/testSalary`,
join: {
from: 'employee.employee_id',
to: 'salary.employee_id',
},
},
};
}
}
module.exports = Employee;
我的猜测是这与引擎盖下对 Knex 的依赖有关。在 the docs 中,有一个代码片段建议通过 Knex 迁移创建表。也许这样做会解决您的问题,因为 Knex 可能会创建一个反对意见期望存在的 id 列。
需要通过在 Model
类.
idColumn
来告知反对意见 id 列是什么
class Salary extends Model {
static get tableName() {
return 'salary';
}
static get idColumn() {
return 'salary_id';
}
// etc
}
使用迁移来创建表将通过创建表并将其 id 列命名为 id
来完成这项工作,但是如果您定义 idColumn
,则可以在没有迁移的情况下使用异议。 =15=]