SequelizeDatabaseError:关系“设备”不存在
Posted
技术标签:
【中文标题】SequelizeDatabaseError:关系“设备”不存在【英文标题】:SequelizeDatabaseError: relation "equipment" does not exist 【发布时间】:2019-10-24 04:01:31 【问题描述】:我正在使用 Sequelize,并且我有以下代码:
迁移文件:
'use strict'
module.exports =
up: (queryInterface, Sequelize) =>
return queryInterface.createTable('equipments',
id:
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true
,
hostname:
type: Sequelize.STRING,
allowNull: false,
unique: true
,
vendor:
type: Sequelize.STRING,
allowNull: false
,
model:
type: Sequelize.STRING,
allowNull: false
,
equipment_class_id:
type: Sequelize.INTEGER,
allowNUll: false,
references: model: 'equipment_classes'
,
created_at:
type: Sequelize.DATE,
allowNull: false
,
updated_at:
type: Sequelize.DATE,
allowNull: false
)
,
down: (queryInterface, Sequelize) =>
return queryInterface.dropTable('equipments')
模型形式设备:
const Model, DataTypes = require('sequelize')
class Equipment extends Model
static init (sequelize)
super.init(
hostname: DataTypes.STRING,
vendor: DataTypes.STRING,
model: DataTypes.STRING,
equipmentClassId:
type: DataTypes.INTEGER,
field: 'equipment_class_id'
,
sequelize
)
module.exports = Equipment
在数据库中,创建了名为设备的表。
CREATE TABLE public.equipments
(
id integer NOT NULL DEFAULT nextval('equipments_id_seq'::regclass),
hostname character varying(255) COLLATE pg_catalog."default" NOT NULL,
vendor character varying(255) COLLATE pg_catalog."default" NOT NULL,
model character varying(255) COLLATE pg_catalog."default" NOT NULL,
equipment_class_id integer,
created_at timestamp with time zone NOT NULL,
updated_at timestamp with time zone NOT NULL,
CONSTRAINT equipments_pkey PRIMARY KEY (id),
CONSTRAINT equipments_hostname_key UNIQUE (hostname)
,
CONSTRAINT equipments_equipment_class_id_fkey FOREIGN KEY (equipment_class_id)
REFERENCES public.equipment_classes (id) MATCH SIMPLE
ON UPDATE NO ACTION
ON DELETE NO ACTION
)
当我运行这段代码时:
async index (req, res)
const equipments = await Equipment.findAll()
return res.json(equipments)
我收到错误:
Executing (default): SELECT "id", "hostname", "vendor", "model", "equipment_class_id" AS "equipmentClassId", "created_at" AS "createdAt", "updated_at" AS "updatedAt" FROM "equipment" AS "Equipment";
(node:17032) UnhandledPromiseRejectionWarning: SequelizeDatabaseError: relation "equipment" does not exist
我的数据库:Postgresql 12
我有其他类似创建的表,但我没有这个问题,我做错了什么?
保罗
【问题讨论】:
【参考方案1】:您的问题是命名。 Sequelize 使用自动将模型名称重写为数据库表。
在您的迁移中,您有:
return queryInterface.createTable('equipments'...
但是你的模型有:
class Equipment extends Model
命名约定将模型中的名称复数以在数据库中查找。所以你应该在你的迁移文件中:
return queryInterface.createTable('Equipment'...
设备是单数设备的复数形式。
您还可以使用sequelize-cli
来简化此操作。
使用sequelize-cli
,您不必担心命名约定。只需运行(用您的属性列表替换属性列表):
sequelize model:generate --name Equipment --attributes fun:string,done:integer
它应该为你处理多元化,生成一个迁移和模型文件。
【讨论】:
另外,您可能需要在createTable
中将其定义为equipment
而不是Equipment
。如果您不使用 sequlize-cli 进行设置,我不确定它将如何重写大写字母。以上是关于SequelizeDatabaseError:关系“设备”不存在的主要内容,如果未能解决你的问题,请参考以下文章
SequelizeDatabaseError:无法截断外键约束中引用的表
SequelizeDatabaseError:运算符不存在:字符变化 [] @> 字符变化
收到以下错误:“SequelizeDatabaseError 列“createdAt”不存在”
未处理的拒绝 SequelizeDatabaseError:无法添加外键约束