Hallo ich bin neu mit Sequelize und ich bin wirklich verloren, wie man Beziehungen verwaltet. Ich möchte 1: N-Beziehung festlegen, aber wenn ich die Ergebnisse sehe, erhalte ich die Beziehungsdaten nicht. Ich arbeite mit 2 Tabellen in diesem Moment, medicos
und hospitals
wo hospitals
kann viele doctors
haben aber doctors
hat nur eine hospital
.Sequelize: Fehler beim Verschachteln der Assoziation
Das ist meine doctors
Tabelle: models/doctors.js
module.exports = function(sequelize, DataTypes) {
return sequelize.define('doctors', {
id: {
type: DataTypes.INTEGER(11),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
name: {
type: DataTypes.STRING,
allowNull: false
},
lastName: {
type: DataTypes.STRING,
allowNull: false
},
SSN: {
type: DataTypes.STRING,
allowNull: false
},
speciality: {
type: DataTypes.ENUM('Doctor','Geriatrician'),
allowNull: false
},
idHospital: {
type: DataTypes.INTEGER(11),
allowNull: false,
},
}, {
tableName: 'doctors',
freezeTableName: true,
classMethods: {
associate: models => {
models.doctors.belongsTo(models.hospitals, {
foreignKey: "idHospital"
})
}
}
});
});
};
und dies ist hospitals
ein: models/hospitals.js
module.exports = function(sequelize, DataTypes) {
return sequelize.define('hospitals', {
id: {
type: DataTypes.INTEGER(11),
allowNull: false,
primaryKey: true,
autoIncrement: true
},
name: {
type: DataTypes.STRING,
allowNull: false
},
idData: {
type: DataTypes.INTEGER(11),
allowNull: false,
references: {
model: 'data',
key: 'id'
}
},
}, {
tableName: 'hospitals',
freezeTableName: true,
classMethods: {
associate: models => {
models.hospitals.hasMany(models.doctors, {
foreignKey: "idHospital"
})
}
}
});
};
In Verwaltung meiner Modelle mit dieser Datei models/index.js
'use strict'
module.exports = (connection) => {
const Users = connection.import('users'),
States = connection.import('states'),
Cities = connection.import('cities'),
Data = connection.import('data'),
Patient = connection.import('patients'),
Hospitals = connection.import('hospitals'),
Doctors = connection.import('doctors'),
Doctors.belongsTo(Hospitals)
Hospitals.hasMany(Doctors)
require('../controllers/patients')(Users)
require('../controllers/personal')(Doctors, Hospitals)
}
dieses ist mein controller
/controllers/personal.js
'use strict'
module.exports = (Doctors) => {
const express = require('express'),
router = express.Router()
router
.get('/', (req, res) => {
Doctors.findAll({ include: [{ model: Hospitals, include: [Doctors], }], }).then((Doctors) => console.log(Doctors));
})
und mein Haupt index
'use strict'
const express = require('express'),
path = require('path'),
bodyParser = require('body-parser'),
Sequelize = require('sequelize'),
port = process.env.PORT || 3000,
app = express(),
connection = new Sequelize('Matadero', 'root', 'root');
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended:true}))
app.set('view engine', 'ejs')
app.set('views', path.resolve(__dirname, 'client', 'views'))
app.use(express.static(path.resolve(__dirname, 'client')))
app.get('/', (req, res) => {
res.render('admin/index.ejs')
})
const onListening =() => console.log(`Successful connection at port: ${port}`)
require('./models')(connection)
app.listen(port, onListening)
const patients = require('./controllers/patients'),
personal = require('./controllers/personal')
app.use('/api/patients', patients)
app.use('/api/personal', personal)
Der Fehler ich erhalte: Unhandled rejection Error: hospitals is not associated to doctors!
Funktioniert immer noch nicht. :(Ich habe gerade meinen Code aktualisiert. Dies ist der Fehler, den ich bekomme "Unbehandelte Ablehnung Fehler: Krankenhäuser ist nicht mit Ärzten verbunden!" –
Siehe meine aktualisierte Antwort. – grimurd
Vielen Dank, Herr GrimurD.Sie haben mir sehr geholfen ! Ihre Antwort ist richtig, ich musste nur ein paar Sachen hinzufügen 'Krankenhäuser.hasMany (Ärzte, {Fremdschlüssel:" idHospital "}) Ärzte.angleTo (Krankenhäuser, {wie: "Krankenhäuser", Fremdschlüssel: "idHospital"}) 'Wenn ich dich nicht belästige, möchte ich, wenn Sie zu Ihrer Antwort hinzufügen und natürlich, Sie haben das Kopfgeld. :) –