如何在 PostgreSQL 中从另一个 table 插入对 UUID 的引用?
How to INSERT a reference to UUID from another table in PostgreSQL?
我正在学习使用 Sequelize 与 PostgreSQL 数据库一起使用。以下所有内容都发生在开发人员身上。环境。这是在手动尝试将数据插入我的 table 以检查是否通过 Sequelize 正确设置、检查失败的单元测试等时发生的
我用 Sequelize 模型制作了两个 table:用户和发布。这两个 table 都在生成 UUIDv4。我已关联用户 hasMany
出版物和出版物 belongsTo
用户(您可以参考额外信息)。
在我的 psql
shell 上,我向我的用户 table 插入了以下记录(为简洁起见删除了其余数据):
| id | firstName | lastName | ..|
|----------------------------------------|------------|-----------|---|
| 8c878e6f-ee13-4a37-a208-7510c2638944 | Aiz | .... |...|
现在,我正在尝试在我的出版物 table 中插入一条记录,同时引用上面新建的用户。这是我在 shell:
中输入的内容
INSERT INTO "Publications"("title", "fileLocation", ..., "userId")VALUES('How to Pasta', 'www.pasta.com', ..., 8c878e6f-ee13-4a37-a208-7510c2638944);
它失败了,我收到以下错误:
ERROR: syntax error at or near "c878e6f"
LINE 1: ...8c878e6f-ee...
(它指向第 1 行参考中终端上的第二个字符 - 'c')。
这是怎么回事?如果我们想在 psql 中手动输入 UUID,是否应该以其他方式输入?我们是否将引用的 UUID 粘贴为字符串?我自己的研究中是否缺少正确的方法?
一些额外的信息,如果有帮助的话:
来自我的模型:
Publication.associate = function(models) {
// associations can be defined here
Publication.belongsTo(models.User, {
foreignKey: "userId"
});
};
和
User.associate = function(models) {
// associations can be defined here
User.hasMany(models.Publication, {
foreignKey: "userId",
as: "publications"
});
};
这是我在出版物中定义 userId
的方式:
userId: {
type: DataTypes.UUID,
references: {
model: "User",
key: "id",
as: "userId"
}
}
如果值得的话,我的 (primaryKey) id
在两个模型上都是 type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4
(我不知道这是否是一个问题)。
用撇号包围您的 uuid(将其写成字符串),pg 会将其转换为 uuid
用 {} 开始和结束字符串是可选的
例如
INSERT INTO "Publications"("title", "fileLocation", ..., "userId")VALUES('How to Pasta', 'www.pasta.com', ..., '8c878e6f-ee13-4a37-a208-7510c2638944');
或
INSERT INTO "Publications"("title", "fileLocation", ..., "userId")VALUES('How to Pasta', 'www.pasta.com', ..., '{8c878e6f-ee13-4a37-a208-7510c2638944}');
来源(我对 pgsql 的了解不多,所以我找了另一个写了一些可用的 pgsql 的人。如果这对你不起作用,请告诉我,我会删除答案):
我正在学习使用 Sequelize 与 PostgreSQL 数据库一起使用。以下所有内容都发生在开发人员身上。环境。这是在手动尝试将数据插入我的 table 以检查是否通过 Sequelize 正确设置、检查失败的单元测试等时发生的
我用 Sequelize 模型制作了两个 table:用户和发布。这两个 table 都在生成 UUIDv4。我已关联用户 hasMany
出版物和出版物 belongsTo
用户(您可以参考额外信息)。
在我的 psql
shell 上,我向我的用户 table 插入了以下记录(为简洁起见删除了其余数据):
| id | firstName | lastName | ..|
|----------------------------------------|------------|-----------|---|
| 8c878e6f-ee13-4a37-a208-7510c2638944 | Aiz | .... |...|
现在,我正在尝试在我的出版物 table 中插入一条记录,同时引用上面新建的用户。这是我在 shell:
中输入的内容INSERT INTO "Publications"("title", "fileLocation", ..., "userId")VALUES('How to Pasta', 'www.pasta.com', ..., 8c878e6f-ee13-4a37-a208-7510c2638944);
它失败了,我收到以下错误:
ERROR: syntax error at or near "c878e6f"
LINE 1: ...8c878e6f-ee...
(它指向第 1 行参考中终端上的第二个字符 - 'c')。
这是怎么回事?如果我们想在 psql 中手动输入 UUID,是否应该以其他方式输入?我们是否将引用的 UUID 粘贴为字符串?我自己的研究中是否缺少正确的方法?
一些额外的信息,如果有帮助的话:
来自我的模型:
Publication.associate = function(models) {
// associations can be defined here
Publication.belongsTo(models.User, {
foreignKey: "userId"
});
};
和
User.associate = function(models) {
// associations can be defined here
User.hasMany(models.Publication, {
foreignKey: "userId",
as: "publications"
});
};
这是我在出版物中定义 userId
的方式:
userId: {
type: DataTypes.UUID,
references: {
model: "User",
key: "id",
as: "userId"
}
}
如果值得的话,我的 (primaryKey) id
在两个模型上都是 type: DataTypes.UUID, defaultValue: DataTypes.UUIDV4
(我不知道这是否是一个问题)。
用撇号包围您的 uuid(将其写成字符串),pg 会将其转换为 uuid
用 {} 开始和结束字符串是可选的
例如
INSERT INTO "Publications"("title", "fileLocation", ..., "userId")VALUES('How to Pasta', 'www.pasta.com', ..., '8c878e6f-ee13-4a37-a208-7510c2638944');
或
INSERT INTO "Publications"("title", "fileLocation", ..., "userId")VALUES('How to Pasta', 'www.pasta.com', ..., '{8c878e6f-ee13-4a37-a208-7510c2638944}');
来源(我对 pgsql 的了解不多,所以我找了另一个写了一些可用的 pgsql 的人。如果这对你不起作用,请告诉我,我会删除答案):