簡體   English   中英

Sequelize.js findAll與include一起搜索foreign_key使頁面從不加載

[英]Sequelize.js findAll with include to search foreign_key makes page never load

描述

我有一個簡單的數據庫,由客戶,地址和訂單組成。 我正在嘗試查找地址為某個字符串的所有訂單。 例如,我可以提供城市或州,而我想基於此查找。

訂單表: 在此處輸入圖片說明

地址表: 在此處輸入圖片說明

訂單型號:

module.exports = db.define('orders', {
    id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
    },
    repId: { type: Sequelize.INTEGER },
    totalItemCost: { type: Sequelize.DECIMAL(10,2), allowNull: false},
    shippingCost: { type: Sequelize.DECIMAL(10,2), allowNull: false},
    orderDate: { type: Sequelize.DATE, allowNull: false},
    isPaid: { type: Sequelize.BOOLEAN, allowNull: false},
    taxPercentage: { type: Sequelize.DECIMAL(10,4), allowNull: false},
}, {timestamps: false, freezeTableName: true, tableName: 'Orders'});

地址模型:

module.exports = db.define('address', {
    id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true
    },
    firstName: { type: Sequelize.STRING, allowNull: false},
    lastName: { type: Sequelize.STRING, allowNull: false},
    city: { type: Sequelize.STRING, allowNull: false},
    address: { type: Sequelize.STRING(256), allowNull: false},
    zip: { type: Sequelize.STRING(10), allowNull: false},
}, {timestamps: false, freezeTableName: true, tableName: 'Address'});

關系:

var models = {};
models.Address = require("./models/address.js");
models.Customer = require("./models/customer.js");
models.Orders = require("./models/orders.js");

// Orders Relations
models.Orders.belongsTo(models.Customer, { foreignKey: { name: 'customerId', allowNull: false }});
models.Orders.belongsTo(models.Address, { foreignKey: { name: 'shippingAddressId', allowNull: false }});

問題

舉例來說,我想查找城市為伯奇伍德的所有訂單。 我在findAll命令中使用include,並將模型設置為我的地址,並且因為我專門命名了送貨地址,所以設置了“ as”語句。 這導致頁面無法加載,我不確定自己在做什么錯。 如果我刪除了include命令,我的所有訂單都可以正常加載。 我沒有看到使用“ belongsTo”時包含的任何示例。 使用“ hasMany”時,看起來好像使用了“關聯”而不是“ foreign_key”。 這就是為什么“ as”不起作用的問題嗎?

models.Orders.findAll({
    where: where,
    include: [{
        model: models.Address, 
        as: 'shippingAddressId',
    }] 
}).then(function(orders) { 
    res.json(orders);
}).catch(function(err) {
    res.status(500).json({"error": err});
});

編輯1

我為查詢語句添加了錯誤處理,但是現在只輸出{“ error”:{}}。 錯誤為空白,因此不是很有幫助。 如果我刪除“ as:'shippingAddressId'”行,則會加載模型,並將地址放入json結構中不需要的字段“ address”下。 在其中添加where子句會使它返回一個空對象{}。 我基本上想要包括:[{all:true,nested:true}],但還要過濾關系Address。

顯然查詢中有錯誤。 這就是為什么res.json(orders); 從來沒有打電話。 我看不到代碼中的錯誤處理。 看看http://bluebirdjs.com/docs/api/catch.html 當您發現錯誤時,將其輸出並准確查看發生了什么。

我通過將關聯添加到我的關系中解決了這個問題:

// Orders Relations
models.Orders.belongsTo(models.Customer, { as: 'customer', onDelete: 'CASCADE', foreignKey: { name: 'customerId', allowNull: false }});
models.Orders.belongsTo(models.Address, { as: 'shippingAddress', onDelete: 'CASCADE', foreignKey: { name: 'shippingAddressId', allowNull: false }});
models.Orders.belongsTo(models.PaymentMethod, { as: 'paymentMethod', onDelete: 'CASCADE', foreignKey: { name: 'paymentMethodId', allowNull: false }});
models.OrderItem.belongsTo(models.Orders, { as: 'order', onDelete: 'CASCADE', foreignKey: { name: 'orderId', allowNull: false }});

然后在查詢中使用該關聯名稱:

models.Orders.findAll({
    where: where,
    include: [{
        model: models.Address,
        as: 'shippingAddress',
        where: {
            city: {like: "%Wedgewood%" }
        }
    }]
}).then(function(orders) { 
    res.json(orders);
}).catch(function(err) {
    res.status(500).json({"error": err});
});

我的印象是該關聯是自動建立的。

暫無
暫無

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM