gpt4 book ai didi

PostgreSQL - 连接表的 SequelizeDatabaseError(代码 : 42P01)

转载 作者:行者123 更新时间:2023-11-29 11:29:13 30 4
gpt4 key购买 nike

我正在尝试为多对多关系创建一个连接表。

我收到以下错误:

"name": "SequelizeDatabaseError",
"parent": {
"name": "error",
"length": 110,
"severity": "ERROR",
"code": "42P01",
"position": "13",
"file": "parse_relation.c",
"line": "1160",
"routine": "parserOpenTable",
"sql": "INSERT INTO \"user_routes\" (\"id\",\"userId\",\"routeId\",\"createdAt\",\"updatedAt\") VALUES (DEFAULT,'1','1','2017-11-15 03:57:21.791 +00:00','2017-11-15 03:57:21.791 +00:00') RETURNING *;"
},

UserRoute之间的关系:

module.exports = (sequelize, DataTypes) => {
const Route = sequelize.define("Route", {
open: {
type: DataTypes.BOOLEAN,
allowNull: true
}
});

Route.associate = models => {
Route.belongsToMany(models.User, {
through: "userRoutes",
as: "users"
});
};
return Route;
};


module.exports = (sequelize, DataTypes) => {
var User = sequelize.define("User", {
email: DataTypes.TEXT,
password: DataTypes.TEXT
});

User.associate = models => {
User.belongsToMany(models.Route, {
through: "userRoutes",
as: "routes"
});
};

return User;
};

用户和路由的迁移文件并不多,只是一些基础知识。对于连接表:

"use strict";
module.exports = {
up: (queryInterface, Sequelize) => {
return queryInterface.createTable("user_route", {
id: {
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: Sequelize.INTEGER
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
},
userId: {
type: Sequelize.INTEGER,
onDelete: "CASCADE",
references: {
model: "Users",
key: "id"
}
},
routeId: {
type: Sequelize.INTEGER,
onDelete: "CASCADE",
references: {
model: "Routes",
key: "id"
}
}
});
},
down: (queryInterface, Sequelize) => {
return queryInterface.dropTable("user_route");
}
};

我的 Controller 和路由如下:

  create(req, res) {
return UserRoutes.create({
userId: req.body.userId,
routeId: req.body.routeId
})
.then(userRoute => res.status(201).send(userRoute))
.catch(err => res.status(400).send(err));
},



app.post("/api/userRoutes", userRoutesController.create);

因此,当我尝试发布到该路由时,我在帖子顶部收到错误消息。

最佳答案

我有同样的错误,并且同样被一个不相关的问题抛出(我的 SQL 子句有单引号,但都被转义了,而且,由于查询是在我删除它们时运行的,所以我专注于删除转义的方法).我的问题是我的迁移是使用违反 PostgreSQL 命名约定的表名生成的;当我更正这个问题时(我的表名中有一个大写的首字母,所以我编辑了一个小写首字母的迁移并重新运行它),错误消失了。也许这可以解决您的问题?

关于PostgreSQL - 连接表的 SequelizeDatabaseError(代码 : 42P01),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47299181/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com