【问题标题】:Sequelize Postgres Query on JSONB object generating invalid query在 JSONB 对象上生成无效查询的 Sequelize Postgres 查询
【发布时间】:2019-03-05 20:55:17
【问题描述】:

将用户定义为 json b 的 Book

sequelize.define(
    "Book",
    {
      id: { type: DataTypes.STRING, primaryKey: true },
      name: DataTypes.STRING,
      genre: DataTypes.STRING,
      user: DataTypes.JSONB
    },
    {
      createdAt: false,
      updatedAt: false,
      timestamps: false,
      tableName: "Book",
      paranoid: true,
      schema: "public"
    }
  ); 
  };

查询以查找 user.id 等于 2 的所有位置

Book.findAll({
      where: {
        "user.id": {
          [Op.eq]: "2"
        }
      }
    })

Sequelize 正在生成如下查询

SELECT id, name, genre, user, authorId FROM public.Book AS Book WHERE 

("Book.user"#>>'{id}')

 = '2';

导致以下错误

错误:列“Book.user”不存在第 1 行:...e,用户,作者 ID FROM public.Book AS Book WHERE ("Book.user... ^

在使用 sequelize 根据用户对象的 id 进行查询时需要一些帮助

【问题讨论】:

    标签: postgresql sequelize.js


    【解决方案1】:

    让我知道这是否有效,我认为没有必要使用 [Op.eq]。

     Book.findAll({
         where: {
            id :  "2"
           }
         })
         .then(result=>{
            console.log(result);
         })
         .catch(error=>{
            console.log(error)
        });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      • 2021-02-18
      • 2018-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-20
      相关资源
      最近更新 更多