我如何在使用sequelize的包含模型中使用限制

2024-06-19

我想从关注模型中获取限制为 2 的用户图像。

Models

const Follow = connector.define('Follow', {
    no: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
    },
    follower_id: {
        type: Sequelize.INTEGER,
        allowNull: true
    },
    target_id: {
        type: Sequelize.INTEGER,
        allowNull: true
    },
    isDelete: {
        type: Sequelize.BOOLEAN,
        allowNull: false
    },
    create_dt,
    delete_dt
}

const User = connector.define('User', {
    no: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
    },
    username: {
        type: Sequelize.STRING,
        allowNull: false

    },
    email: {
        type: Sequelize.STRING,
        allowNull: false
    },
    password: {
        type: Sequelize.STRING,
        allowNull: false
    },
    profile_img: {
        type: Sequelize.STRING,
        allowNull: true
    },
    bio: {
        type: Sequelize.STRING,
        allowNull: true
    },
    phone: {
        type: Sequelize.STRING,
        allowNull: true
    },
    gender: {
        type: Sequelize.STRING,
        allowNull: true
    },
    website: {
        type: Sequelize.STRING,
        allowNull: true
    },
    isDelete: {
        type: Sequelize.BOOLEAN,
        allowNull: false
    },
    create_dt,
    update_dt,
    delete_dt
}

const Image = connector.define('Image', {
    no: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
    },
    file: {
        type: Sequelize.STRING,
        allowNull: false
    },
    location: {
        type: Sequelize.STRING,
        allowNull: true
    },
    caption: {
        type: Sequelize.STRING,
        allowNull: true
    },
    tags: {
        type: Sequelize.STRING,
        allowNull: true
    },
    isDelete: {
        type: Sequelize.BOOLEAN,
        allowNull: false
    },
    create_dt,
    update_dt,
    delete_dt,
    user_id: {
        type: Sequelize.INTEGER,
        allowNull: true
    }
}

并且,加入

User.hasMany(Image, {foreignKey: 'user_id'})
Image.belongsTo(User, {foreignKey: 'user_id'})

User.hasMany(Follow, {foreignKey: 'follower_id'})
Follow.belongsTo(User, {foreignKey: 'follower_id'})

User.hasMany(Follow, {foreignKey: 'target_id'})
Follow.belongsTo(User, {foreignKey: 'target_id'})

所以,我尝试通过使用 include 来获取用户的图像。

const followerImages = await Follow.findAll({
            attributes: ['target_id'],
            where:{
                follower_id: loginUser_id
            },
            include:[
                {
                    model: User,
                    required: true,
                    attributes: ['username', 'email', 'profile_img'],
                    include:[
                        {
                            model: Image,
                            required: true
                        }
                    ]
                }
            ]
        })

但我想在限制 2 下获取图像。

所以我尝试了

const followerImages = await Follow.findAll({
            attributes: ['target_id'],
            where:{
                follower_id: loginUser_id
            },
            include:[
                {
                    model: User,
                    required: true,
                    attributes: ['username', 'email', 'profile_img'],
                    include:[
                        {
                            model: Image,
                            required: true,
                            limit: 2
                        }
                    ]
                }
            ]
        })

但这会产生我无法理解的错误。

images 字段是一个包含 4 处空对象的数组。

都一样..

问题是什么?

我怎么解决这个问题??


你可以试试 :

include:[
    {
        model: Image,
        attributes : ['id','user_id','image'] , // <---- don't forget to add foreign key ( user_id )
        separate : true, // <--- Run separate query
        limit: 2
    }
]

Limit有时会在嵌套级别上导致问题,因此单独运行该查询总是安全的。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

我如何在使用sequelize的包含模型中使用限制 的相关文章

随机推荐

  • 如果字符串或整数的 getHashCode() 不能保证唯一,为什么要使用它?

    正如我在标题中所写 如果在应用程序中使用 getHashCode 不安全 为什么要使用它 对于字符串和整数 我想用它来交叉方法并排除 Linq 模型中的方法 或创建我自己的 IEqualityCompare 类 这感觉像是一个机会 如果它不
  • NetBeans IDE maven项目无法解决依赖关系

    使用 eclipse 多年后 我正在将自己移植到 NetBeans IDE 我正在打开现有的 Maven 项目 一切似乎都正常 但是当我尝试构建该项目时 它会抛出错误 提示 无法解析项目的依赖项 尽管 JAR 文件存在于我的 m2 目录中
  • 为什么 Android WebView 拒绝用户输入?

    我正在开发一个 Android 应用程序 它使用 WebView 来显示 Facebook 的登录页面 该页面加载精美 我可以选择用户名 密码文本框 但在其中输入内容将不起作用 也就是说 它们肯定有输入焦点 它们有橙色焦点突出显示框和闪烁的
  • 在asp net mvc中简单的图像上传

    我正在构建一个简单的学校门户 我一直坚持将图像上传到我的应用程序中 即用户应该将学校图像上传到我的服务器 我的图像目录为 Content Images 所有上传图像都应该上传到这个目录 我有以下代码 input type file id S
  • 关于 inflater.inflate Android 文档的困惑

    我正在研究此链接中的片段 http developer android com guide components fragments html http developer android com guide components frag
  • Sencha Cmd 5 + Java 8 错误

    在我的 Windows 构建服务器上安装 Java 8 JDK 后 执行以下命令时遇到以下错误sencha命令 C gt sencha Error Registry key Software JavaSoft Java Runtime En
  • 如何将列表列表写入 CSV 文件 Python?

    我有一个列表 例如 a b c d e f 我想将其写入 CSV 文件 如下所示 a b c d e f 我怎么做 我尝试过使用 csv writerows 但输出文件的每个字符位于不同的单元格中 并且全部位于同一行中 从某种意义上说 第一
  • Cocos2D复杂动画[关闭]

    Closed 这个问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 我正在使用 Cocos2D 将我的游戏从 Flash 移植到 iOS 我现在有一个工作版本 我很高兴我
  • 使用ftplib进行多线程上传

    我正在尝试进行多线程上传 但出现错误 我猜想也许不可能在 ftplib 中使用多线程 这是我的代码 class myThread threading Thread def init self threadID src counter ima
  • 在汇编中初始化字符串数组

    我想创建一个数据数组 在初始化数据部分保存 5 个字符串 每个字符串正好有 4 个字符 每个字符串都有一些初始数据 例如第一个字符串的 abcd 第二个字符串的 efgh 等等 无效的 0任何字符串都不需要字符 如何用汇编语言初始化字符串数
  • 从存储访问框架 UI 获取文件夹后保存图像

    我设置了一个首选项 让用户使用存储访问框架为我的应用程序选择保存文件夹 获取uri后onActivityResult我将其保存到SharedPreferences作为字符串并在要保存时保存图像 我正在使用此方法成功保存图像 public v
  • Flutter:如何使用 AnimatedContainer 和扩展列?

    假设我们有 3 个孩子Column Flexible flex 1 child Container color Colors red Flexible flex 3 child Container color Colors yellow F
  • 如何在 Ubuntu Karmic 上安装 LFE?

    Erlang 已经安装 dpkg l grep erlang ii erlang 1 13 b 3 dfsg 2ubuntu2 Concurrent real time distributed function ii erlang appm
  • 在响应模式下使用 CSS 更改元素顺序

    图1为桌面模式 下面两张图片和文字 总共三个div 图 2 是我希望它在移动浏览器 例如手机 中的显示方式 关于如何实现这一点有什么想法吗 我愿意接受任何建议 这个想法是让文本显示在图像上方 以最好地说明这两个图像的描述 在桌面版本中将文本
  • 无法删除 Access 中 SQL 表上的注册表

    我有一个在 Access 应用程序中链接的 SQL Server 表 如果我尝试使用删除查询删除记录 则没有问题 但是 如果我尝试直接在表中删除记录或在数据表模式下使用选择查询 Access 不允许我删除记录并引发以下警告 Microsof
  • 如何在 RequireJS 中模拟单元测试的依赖关系?

    我有一个要测试的 AMD 模块 但我想模拟它的依赖项 而不是加载实际的依赖项 我正在使用 requirejs 我的模块的代码如下所示 define hurp durp function Hurp Durp return foo functi
  • 如何获取核心数据中现有实体(表)的列表

    如何获取核心数据中特定模式 托管对象模型 的现有实体 表 列表 我刚刚开始实施核心数据概念并坚持这些要点 就像是 SELECT COUNT FROM information schema tables WHERE table schema
  • 当我启动 Windows 命令提示符时,我做了什么导致环境变量发生更改?

    我使用的是 Windows 10 x64 我安装了 Anaconda3 如果我启动 C Windows system32 cmd exe 时没有运行任何其他内容 并且在我可以看到的后台中没有任何有趣的内容 则以下内容将添加到控制面板 UI
  • 诊断“功能主机未运行。”在码头工人

    我正在尝试将几个基于 dotnet 的功能应用程序 v3 迁移到 docker 容器 为此 我们使用来自mcr microsoft com azure functions dotnet https hub docker com micros
  • 我如何在使用sequelize的包含模型中使用限制

    我想从关注模型中获取限制为 2 的用户图像 Models const Follow connector define Follow no type Sequelize INTEGER primaryKey true autoIncremen