代码之家  ›  专栏  ›  技术社区  ›  Shooting Stars

alias join Sequelize上“字段列表”中的未知列

  •  0
  • Shooting Stars  · 技术社区  · 2 年前

    这是我的主要模型定义:

    const Sequelize = require('sequelize') 
    
    // Import sequelize object,  
    // Database connection pool managed by Sequelize. 
    const sequelize = require('./database.js') 
    
    const CartTransaction = require('./CartTransaction');
    const PayPalOrder = require('./PayPalOrder')
    
    const CartTransactionPayPal = sequelize.define('cart_transaction_paypal', {
    
    cart_transaction_paypal_id: {
    
        type:Sequelize.INTEGER, 
    
        // To increment user_id automatically. 
        autoIncrement:true, 
        allowNull:false, 
        primaryKey:true
    }, 
    
    status: {type: Sequelize.STRING, allowNull: false}
    
    });
    
    CartTransactionPayPal.belongsTo(CartTransaction, {as: 'paypal_transactions', foreignKey: 'cart_transaction_id'});
    CartTransaction.hasOne(CartTransactionPayPal, {as: 'paypal_transactions', foreignKey: 'cart_transaction_id'});
    
    CartTransactionPayPal.belongsTo(PayPalOrder, {as: 'paypal_orders', foreignKey: 'paypal_order_id'})
    PayPalOrder.hasOne(CartTransactionPayPal, {as: 'paypal_orders', foreignKey: 'paypal_order_id'})
    

    当我尝试执行此连接操作时:

    await PayPalOrder.findAll({
            include: [{
                model: CartTransactionPayPal,
                as: 'paypal_orders',
                required: true,
                include: [{
                    model: CartTransaction,
                    as: 'paypal_transactions',
                    required: true
    

    我得到这个错误:

    "Unknown column 'paypal_transactions.cart_transaction_id' in 'field list'",
    

    但为什么会发生这种情况。它不应该加入CartTransaction PayPal on CartTransaction,因为它是嵌套的包含吗?我错过什么了吗?嵌套include语句的语法是否错误?

    0 回复  |  直到 2 年前