Sequelize: Error: Error: Table1 is not associated to Table2

前端 未结 1 544
野性不改
野性不改 2021-01-17 14:26

I am trying to create the following associations using sequelize but I keep getting the following error “Error: Error: customer is not associated to order!”. I have bi-d

1条回答
  •  被撕碎了的回忆
    2021-01-17 15:04

    The problem is probably with how you are setting you association, kindly mention your strategy.

    Following is working fine if you use the express index.js file setup and then query http://docs.sequelizejs.com/en/1.7.0/articles/express/

    'use strict';
    module.exports = function(sequelize, DataTypes) {
      var customer = sequelize.define('customer', {
        customernumber: DataTypes.STRING(30), //remove
        customerspecificationid: DataTypes.INTEGER,
        customertypeid: DataTypes.INTEGER,
        sportid: DataTypes.INTEGER,
        customername: DataTypes.STRING(20), //remove
        address: DataTypes.STRING(30),
        city: DataTypes.STRING(30),
        state: DataTypes.STRING(30),
        zipcode: DataTypes.STRING(30),
        ordercomplete: DataTypes.BOOLEAN,
        isactive: DataTypes.BOOLEAN
      }, {
          associate: function(models) {
              // associations can be defined here
            models.customer.hasMany(models.order);
          }
      });
    
      customer.hook('afterCreate', function(cust, options) {
          //generate the customer number
    
            return customer.update({ customernumber: custnumber }, {
            where: {
              id: cust.id
            }
          });
      });
    
      return customer;
    };
    
    
    'use strict';
    module.exports = function(sequelize, DataTypes) {
      var order = sequelize.define('order', {
        ponumber: DataTypes.STRING(30), //remove
        orderdate: DataTypes.DATE,
        shippingmethod: DataTypes.STRING(30),
        shippingterms: DataTypes.STRING(30),
        deliverydate: DataTypes.DATE,
        paymentterms: DataTypes.STRING(30),
        overridediscount: DataTypes.BOOLEAN,
        shippingaddress: DataTypes.STRING(30),
        shippingcity: DataTypes.STRING(30),
        shippingstate: DataTypes.STRING(20),
        shippingzipcode: DataTypes.STRING(10),
        isactive: DataTypes.BOOLEAN
      }, {
          associate: function(models) {
            // associations can be defined here
            models.order.belongsTo(models.user);
            models.order.belongsTo(models.customer);
          }
      });
    
      order.hook('afterCreate', function(ord, options) {
          //generate po number
    
          return order.update({ ponumber: ponumbr }, {
            where: {
              id: ord.id
            }//,
            //transaction: options.transaction
          });
      });
    
      return order;
    };
    
    'use strict';
    
    module.exports = function(sequelize, DataTypes) {
      var user = sequelize.define('user', {
        username: DataTypes.STRING(30), //remove
        password: DataTypes.STRING(255),
        emailaddress: DataTypes.STRING(255),
        firstname: DataTypes.STRING(30),
        middlename: DataTypes.STRING(30), //remove
        lastname: DataTypes.STRING(30),
        approve: DataTypes.BOOLEAN,
        roles: DataTypes.STRING(50),
        isactive: DataTypes.BOOLEAN
      }, {
        classMethods: {
          associate: function(models) {
            // associations can be defined here
            models.user.hasMany(models.order);
          }
        }
      });
    
      user.hook('afterCreate', function(usr, options) {
          //hash the password
    
          return user.update({ password: passwd }, {
            where: {
              id: usr.id
            }
          });
      });
    
      return user;
    };
    

    // index.js file where you shall associate the routes

    var fs        = require('fs')
        , path      = require('path')
        , Sequelize = require('sequelize')
        , lodash    = require('lodash')
        , sequelize = new Sequelize('sequelize_test', 'root', 'root')
        , db        = {} 
    
      fs.readdirSync(__dirname)
        .filter(function(file) {
          return (file.indexOf('.') !== 0) && (file !== 'index.js')
        })
        .forEach(function(file) {
          var model = sequelize.import(path.join(__dirname, file))
          db[model.name] = model
        })
    
      Object.keys(db).forEach(function(modelName) {
        if (db[modelName].options.hasOwnProperty('associate')) {
          db[modelName].options.associate(db)
        }
      })
      // sequelize.sync({force: true})
      module.exports = lodash.extend({
        sequelize: sequelize,
        Sequelize: Sequelize
      }, db)
    

    Put the above db code in respective files in db folder or whatever you like to name it and then you can use your query

    var db = require('./db');

    db.order.find({
                where: { id: 0 },
                include: [ db.customer, db.user ]
            })
            .then(function(order){
                console.log(order)
            })
    

    0 讨论(0)
提交回复
热议问题