2017-03-29 2 views
0

sequelize-cliを使用してクエリにテーブルを関連付けるときに問題が発生します。Sequelize association includeがnullを返す

クエリは機能しますが、アドレステーブルには入力されません。患者のみが入力されます。住所配列は無視されます。

私はテーブル間に1対1の関係を作成しましたが、それがエラーの原因であるかどうか、または2つのテーブルを関連付ける別の場所にあるかどうかはわかりません。ここ

は私のモデルである:

サーバー/モデル/ patient.js

module.exports = (sequelize, Sequelize) => { 
    const Patient = sequelize.define('Patient', { 
     /// 
    }, { 
     classMethods: { 
      associate: (models) => { 
       Patient.belongsTo(models.Adresse, { 
        foreignKey: 'adresseId', 
       }); 
      } 
     } 
    }); 
    return Patient; 
}; 

サーバー/モデル/ adresse.js

module.exports = function(sequelize, Sequelize) { 
    const Adresse = sequelize.define('Adresse', { 
     adresse: { 
      type: Sequelize.STRING, 
      allowNull: false, 
     }, 
     complementAdr: { 
      type: Sequelize.STRING 
     }, 
     codePostal: { 
      type: Sequelize.INTEGER, 
      allowNull: false 
     }, 
    }, { 
     classMethods: { 
      associate: (models) => { 
       Adresse.hasMany(models.Patient, { 
        foreignKey: 'adresseId', 
        as: 'Patients', 
       }); 
      } 
     } 
    }); 
    return Adresse; 
}; 

、ここどこですマイグレーションファイルに関連付けを指定しました:

サーバー/移行/ 20170326145609作成-patient.js

adresseId: { 
    type: Sequelize.INTEGER, 
    references: { 
     model: 'Adresses', 
     key: 'id_adresse', 
     as: 'adresseId', 
    }, 
}, 

サーバー/移行/ 20170326145502作成-adresse.js

module.exports = { 
    up: (queryInterface, Sequelize) => { 
     return queryInterface.createTable('Adresses', { 
      id_adresse: { 
       allowNull: false, 
       autoIncrement: true, 
       primaryKey: true, 
       type: Sequelize.INTEGER 
      }, 
      adresse: { 
       type: Sequelize.STRING, 
       allowNull: false, 
      }, 
      complementAdr: { 
       type: Sequelize.STRING 
      }, 
      codePostal: { 
       type: Sequelize.INTEGER, 
       allowNull: false 
      }, 
      createdAt: { 
       allowNull: false, 
       type: Sequelize.DATE 
      }, 
      updatedAt: { 
       allowNull: false, 
       type: Sequelize.DATE 
      } 
     }); 
    }, 
    down: function(queryInterface, Sequelize) { 
     return queryInterface.dropTable('Adresses'); 
    } 
}; 

と最終的にここに私のクエリがあります私のコントローラファイル:

サーバ/コントローラ/ patients.js

const express = require('express'); 
const router = express.Router(); 
const jwt = require('jsonwebtoken'); 
const Patient = require('../models').Patient; 
const Adresse = require('../models').Adresse; 

module.exports = { 
    create(req, res) { 
     return Patient 
      .create({ 
       /// 
       adressesId: { 
        adresse: req.body.adresse, 
        codePostal: req.body.codePostal, 
       } 
      }, { 
       include: [{ 
        model : Adresse 
       }] 
      }) 
      .then(patient => res.status(201).send(patient)) 
      .catch(error => res.status(400).send(error)); 
    } 
}; 

答えて

1

Patient

return Patient.create({ 
    // patient attributes, 
    Adresse: { 
     adresse: req.body.adresse, 
     codePostal: req.body.codePostal 
    }, 
    include: [ Adresse ] 
}).then(patient => { 
    // look at the query generated by this function 
    // it should create both patient and adresse 
}); 
所与に関する Adresseモデルインスタンスを作成する際に熱心代わり adresseId Adresseを使用してみてください
関連する問題