簡體   English   中英

Sequelize include 不適用於關聯表

[英]Sequelize include is not working with associated table

我正在使用 sequelize 5.21.2

我有一個用戶模型:

'use strict';
module.exports = (sequelize, Sequelize) => {
  const users = sequelize.define('users', {
    id: {
      type: Sequelize.INTEGER,
      primaryKey: true,
      autoIncrement: true,
      allowNull: false
    },
    firstname: {
      type: Sequelize.STRING,
      allowNull: false,
    },{
      timestamp: true,
      logging: console.log,
  });
  users.associate = function(models) {
    models.users.hasMany(models.messages);
  };
  return users;
};

消息模型:

'use strict';
module.exports = (sequelize, Sequelize) => {
    const messages = sequelize.define('messages', {
        id: {
            type: Sequelize.INTEGER,
            primaryKey: true,
            autoIncrement: true,
            allowNull: false
        },
        message:{
            type: Sequelize.TEXT,
            allowNull: false
        }
    }, {
        timestamp: true,
        logging: console.log,
        scopes:{
            withUser:{
                include: [
                    {
                        model: sequelize.models.users
                    }
                ]
            }
        }
    });
    messages.associate = function(models) {
        models.messages.belongsTo(models.users);
    };
    return messages;
};

我的控制器:

const db = require('../models');
const Messages = db.messages
exports.getOneMessage = (req, res) =>{
    Messages.scope(['withUser']).findByPk(req.query.id).then(message => {
        res.status(200).json({
            "description": "getOne - " + req.query.id,
            "message": message
        });
    }).catch(err => {
        res.status(500).json({
            "description": "Can not access",
            "error": err
        });
    });
};

我有這個錯誤:

Error: Include unexpected. Element has to be either a Model, an Association or an object.
  • 我已經嘗試在我的用戶模型中使用 hasOne 而不是 hasMany - 沒有變化
  • 我在我的用戶模型中嘗試過 hasMany 並刪除了消息模型的belongsTo - 沒有變化
  • 我已經嘗試在消息模型中使用belongsTo 並刪除了用戶模型中的hasMany - 沒有變化
  • 我已經刪除了所有表並重試了所有內容 - 沒有變化

我究竟做錯了什么?

似乎問題來自范圍......如果我在控制器本身中包含所有內容,一切都是正確的。 我在我的項目中使用了很多范圍,但唯一給我帶來麻煩的是用戶模型......我不知道為什么。

const db = require('../models');
const Messages = db.messages
const Users = db.users
exports.getOneMessage = (req, res) =>{
    Messages.findByPk(req.query.id, {include:[Users]}).then(message => {
        res.status(200).json({
            "description": "getOne - " + req.query.id,
            "message": message
        });
    }).catch(err => {
        res.status(500).json({
            "description": "Can not access",
            "error": err
        });
    });
};

續集:不相關<table> </table><div id="text_translate"><p>我得到的images is not associated to product! 綁定 model 的關聯時出錯。</p><p> ProductImages與Product相關聯, ProductImages與Images model 相關聯。 所以,我需要通過分配給它來將images屬性渲染到products集合中。</p><p> 我試圖綁定的 model 如下。</p><p> <em>產品.model.ts</em></p><pre> const Product = SQLize.define('product', { id: { type: DataTypes.INTEGER, autoIncrement: true, primaryKey: true } product_title: { type: new DataTypes.STRING(255) }, vendor_id: { type: DataTypes.INTEGER } }); Product.hasMany(ProductImages, {foreignKey: 'product_id', targetKey: 'id', as:'product_img_refs'}) export { Product };</pre><p> <em>產品圖像.model.ts</em></p><pre> const ProductImages = SQLize.define('product_images', { id: { type: DataTypes.INTEGER.UNSIGNED, autoIncrement: true, primaryKey: true, }, product_id: { type: DataTypes.INTEGER }, product_image_id: { type: DataTypes.INTEGER } img_type_id: { type: DataTypes.INTEGER } }); ProductImages.belongsTo(ImagesModel, {foreignKey: 'product_image_id', targetKey: 'id', as:'product_images' }) export {ProductImages}</pre><p> <em>圖像.model.ts:</em></p><pre> const ImagesModel = SQLize.define('images', { id: { type: DataTypes.INTEGER.UNSIGNED, autoIncrement: true, primaryKey: true, }, img_url: { type: DataTypes.STRING } }); export { ImagesModel }</pre><p> 下面是我執行 SQLize 操作的存儲庫文件。</p><pre> public async getProductData() { var prodData = Product.findAll({ include: [ { model: Vendor, as: 'vendor' }, { model: ProductImages, as: 'product_img_refs' } { model: ImagesModel, as: 'product_images' } ] }); return prodData; }</pre><p> =&gt; 示例<a href="https://prnt.sc/t18c2m" rel="nofollow noreferrer">product_images</a>表記錄。</p><p> =&gt; 示例<a href="https://www.awesomescreenshot.com/image/4922903/c732b6d5d27f93117d14949b00666347" rel="nofollow noreferrer">圖像</a>表記錄。</p><p> =&gt;<a href="https://dbdiagram.io/d/5ee8a8bc9ea313663b3a9f4a" rel="nofollow noreferrer">數據庫模式</a>以獲得更多可視化。</p><p> =&gt;我已經檢查了這個<a href="https://stackoverflow.com/a/40698629/5624578" rel="nofollow noreferrer">答案</a>,但它與我的 model 無關,因為我有三個具有不同關聯的模型。</p></div><table></table>

[英]Sequelize: <table> is not associated to <Table>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 Sequelize:關聯表查詢中的“包括意外” sequelize在關聯的模型數據中不包括聯結表 續集:不相關<table> </table><div id="text_translate"><p>我得到的images is not associated to product! 綁定 model 的關聯時出錯。</p><p> ProductImages與Product相關聯, ProductImages與Images model 相關聯。 所以,我需要通過分配給它來將images屬性渲染到products集合中。</p><p> 我試圖綁定的 model 如下。</p><p> <em>產品.model.ts</em></p><pre> const Product = SQLize.define('product', { id: { type: DataTypes.INTEGER, autoIncrement: true, primaryKey: true } product_title: { type: new DataTypes.STRING(255) }, vendor_id: { type: DataTypes.INTEGER } }); Product.hasMany(ProductImages, {foreignKey: 'product_id', targetKey: 'id', as:'product_img_refs'}) export { Product };</pre><p> <em>產品圖像.model.ts</em></p><pre> const ProductImages = SQLize.define('product_images', { id: { type: DataTypes.INTEGER.UNSIGNED, autoIncrement: true, primaryKey: true, }, product_id: { type: DataTypes.INTEGER }, product_image_id: { type: DataTypes.INTEGER } img_type_id: { type: DataTypes.INTEGER } }); ProductImages.belongsTo(ImagesModel, {foreignKey: 'product_image_id', targetKey: 'id', as:'product_images' }) export {ProductImages}</pre><p> <em>圖像.model.ts:</em></p><pre> const ImagesModel = SQLize.define('images', { id: { type: DataTypes.INTEGER.UNSIGNED, autoIncrement: true, primaryKey: true, }, img_url: { type: DataTypes.STRING } }); export { ImagesModel }</pre><p> 下面是我執行 SQLize 操作的存儲庫文件。</p><pre> public async getProductData() { var prodData = Product.findAll({ include: [ { model: Vendor, as: 'vendor' }, { model: ProductImages, as: 'product_img_refs' } { model: ImagesModel, as: 'product_images' } ] }); return prodData; }</pre><p> =&gt; 示例<a href="https://prnt.sc/t18c2m" rel="nofollow noreferrer">product_images</a>表記錄。</p><p> =&gt; 示例<a href="https://www.awesomescreenshot.com/image/4922903/c732b6d5d27f93117d14949b00666347" rel="nofollow noreferrer">圖像</a>表記錄。</p><p> =&gt;<a href="https://dbdiagram.io/d/5ee8a8bc9ea313663b3a9f4a" rel="nofollow noreferrer">數據庫模式</a>以獲得更多可視化。</p><p> =&gt;我已經檢查了這個<a href="https://stackoverflow.com/a/40698629/5624578" rel="nofollow noreferrer">答案</a>,但它與我的 model 無關,因為我有三個具有不同關聯的模型。</p></div><table></table> sequelize表與用戶無關 BelongToMany與Sequelize表無關 續集 - 關聯表上的 Select SequelizeEagerLoadingError:關聯在續集中不起作用 在續集上,“findOne”的“include”不起作用 合並,合並兩個關聯表 Sequelize include 不適用於 hasOne 關系
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM