Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Postgresql 续集外键_Postgresql_Associations_Sequelize.js - Fatal编程技术网

Postgresql 续集外键

Postgresql 续集外键,postgresql,associations,sequelize.js,Postgresql,Associations,Sequelize.js,我想与sequelize db:postgress进行内部连接 我有2个数据库(用户和帖子) 使用者 职位 控制器 export const listAllPosts = async params => { const { offset } = params; try { const allPosts = await Post.findAll({ // limit: 20, // offset: offset

我想与sequelize db:postgress进行内部连接 我有2个数据库(用户和帖子)

使用者

职位

控制器

export const listAllPosts = async params => {
    const { offset } = params;

    try {
        const allPosts = await Post.findAll({
            // limit: 20,
            // offset: offset ? offset * 20 : 0,
            // order: [["id", "DESC"]],
            attributes: ["title", "content","tags","description","createdAt","updatedAt"],
            required: true,
            include:[
                {
                    model:User,
                    attributes: ["username", "image"],
                    required:true,
                }
            ]
        });
        return allPosts;
    } catch (error) {
        throw error;
    }
};
当我收集api时,显示错误:

正在执行(默认):选择“发布”、“id”、“发布”、“标题”, “帖子”“内容”“帖子”“标签”“帖子”“说明”, 将“.createdat”张贴为“createdat”,将“.updatedat”张贴为“updatedat”, “用户”,“id”为“用户.id”,“用户”,“用户名”为“用户.用户名”, “用户”。“image”作为“user.image”从“posts”作为“post”内部联接 “用户”作为“发布”上的“用户”。“作者”=“用户”。“id”; 错误:SequelizeDatabaseError:运算符不存在:text=integer

但我想要精确显示,我不想更改primaryKey:

SELECT "post"."title", "post"."content", "post"."tags", "post"."description", "post"."createdat"AS "createdAt", "post"."updatedat" AS "updatedAt", "user"."username" AS "user.username", "user"."image" AS "user.image" FROM "posts" AS "post" INNER JOIN "users" AS "user" ON "post"."author" = "user"."username";

我该怎么做呢?

你最好使用“用户ID”而不是“作者”。它会解决你的问题。 编辑协会

User.hasMany(Post, { foreignKey: "userId", sourceKey: "id" });
Post.belongsTo(User, { foreignKey: "id", sourceKey: "userId" });
并建议你阅读

我已经解决了User.hasMany(Post,{foreignKey:“author”,sourceKey:“username”})Post.belongsTo(User,{foreignKey:“author”,targetKey:“username”})
SELECT "post"."title", "post"."content", "post"."tags", "post"."description", "post"."createdat"AS "createdAt", "post"."updatedat" AS "updatedAt", "user"."username" AS "user.username", "user"."image" AS "user.image" FROM "posts" AS "post" INNER JOIN "users" AS "user" ON "post"."author" = "user"."username";
User.hasMany(Post, { foreignKey: "userId", sourceKey: "id" });
Post.belongsTo(User, { foreignKey: "id", sourceKey: "userId" });