2013-09-29 15 views
8

Il problema è che non riesco a far funzionare la relazione hasOne, che non impaziente di caricare l'oggetto di tipo stato.Sequenza di associazioni hasOne, appartiene a

Tutte le query vengono eseguite su tabelle esistenti.

Ecco la tabella dei clienti, che cosa è importante è il campo cst_state_type:

module.exports = function(sequelize, DataTypes) { 

    return sequelize.define('customer', { 

     customer: { 
      type: DataTypes.INTEGER, 
      primaryKey: true, 
      autoIncrement: true, 
      allowNull: true, 
      validate: { 
       isNumeric: true 
      } 
     }, 
     first_name: { 
      type: DataTypes.STRING(100), 
      validate: { 
       isAlphanumeric: true 
      } 
     }, 
     last_name: DataTypes.STRING(100), 
     identity_code: { 
      type: DataTypes.STRING(20), 
      allowNull: true, 
      validate: { 
       isNumeric: true 
      } 
     }, 
     note: DataTypes.STRING(1000), 
     birth_date: DataTypes.DATE, 


     created_by: DataTypes.INTEGER, 
     updated_by: DataTypes.INTEGER, 

     cst_type: DataTypes.INTEGER, 
     cst_state_type: { 
      type: DataTypes.INTEGER, 
     } 

    }, { 
     tableName: 'customer', 

     updatedAt: 'updated', 
     createdAt: 'created', 
     timestamps: true 
    }); 
}; 

tavolo cst_state_type:

module.exports = function(sequelize, DataTypes) { 

    return sequelize.define('StateType', { 

     cst_state_type: { 
      type: DataTypes.INTEGER, 
      primaryKey: true, 
      autoIncrement: true, 
      validate: { 
      } 
     }, 
     name: DataTypes.STRING(100), 
    }, { 
     tableName: 'cst_state_type', 
     timestamps: false 
    }); 
}; 

Come vengono descritti i rapporti:

global.db.Customer.hasOne(global.db.StateType, { 
    foreignKey: 'cst_state_type', 
    as: 'state_type' 
    }); 

    global.db.StateType.belongsTo(global.db.Customer, { 
    foreignKey: 'cst_state_type' 
    }); 

E la creazione ansiosa Richiesta di carico:

db.Customer.findAll({ 
     include: [ 
      { model: db.Address, as: 'addresses' }, 
      { model: db.StateType, as: 'state_type' } 
     ] 
    }) 
     .success(function (customers) { 
      res.json(200, customers); 
     }) 
     .fail(function (error) { 
      res.json(500, { msg: error }); 
     }); 

risposta

2

Sono abbastanza sicuro che l'errore è nelle vostre associazioni da qualche parte. Dal modo in cui hai descritto la vostra struttura della tabella, i association dovrebbe essere simile a questa:

global.db.Customer.belongsTo(global.db.StateType, { 
    foreignKey: 'cst_state_type', 
    as: 'state_type' 
}); 

global.db.StateType.hasMany(global.db.Customer, { 
    foreignKey: 'cst_state_type' 
}); 

se ho capito bene, lo stesso stato può essere assegnato a molti clienti, quindi StateType.hasMany. La relazione da cliente -> statetype è una "back-associazione", il che significa che la chiave esterna è nella tabella dei clienti. Per questo è necessario belongsTo

18

Grazie per voi risposta, mi ha aiutato molto. Puoi anche aggiungere le relazioni direttamente nel tuo modello usando classmethods. Ho aggiunto un esempio qui sotto, spero che questo aiuti!

utente modello (file)

module.exports = function(sequelize, DataTypes){ 
    var User = sequelize.define(
     'User', { 
      name: { 
       type: DataTypes.STRING, 
       allowNull: false 
      } 
     }, 
     { 
      classMethods:{ 
       associate:function(models){ 
        User.hasMany(models.Comment, { foreignKey: 'userId'}); 
       } 
      } 
     } 

    ); 
    return User; 
}; 

Commento Model (file):

module.exports = function(sequelize, DataTypes){ 
    var Comment = sequelize.define(
     'Comment', { 
      text: { 
       type: DataTypes.STRING, 
       allowNull: false 
      } 
     }, 
     { 
      classMethods:{ 
       associate:function(models){ 
        Comment.belongsTo(models.User, { foreignKey:'userId'}); 
       } 
      } 
     } 

    ); 
    return Comment; 
}; 

Non è necessario impostare la ForeignKey, sequelize gestirà se si don 'specificare i tasti stranieri.

Poi nella query:

models.Comment.find({ 
     where: { id: id }, 
     include: [ 
      models.User 
     ], 
     limit: 1 
    }) 
+0

eccellente esempio! Semplice e funzionale. –

Problemi correlati