Vote count:
0
Here is a simplified version of my schema:
// Collleges
var Colleges = sequelize.define("colleges", {
collegeId: {
type: Sequelize.INTEGER,
autoIncrement: true,
primaryKey: true
},
collegeCode: {
type: Sequelize.STRING
unique: true
},
collegeName: {
type: Sequelize.STRING
}
});
// College -> Departments
var Departments = sequelize.define("departments", {
departmentId: {
type: Sequelize.INTEGER,
autoIncrement: true,
primaryKey: true
},
collegeId: {
type: Sequelize.INTEGER,
references: Colleges,
referencesKey: "collegeId",
unique: "collegeDep"
},
depCode: {
type: Sequelize.STRING,
unique: "collegeDep"
},
depName: {
type: Sequelize.STRING
}
});
My problem: When I try to invoke an eager load ("join" between Colleges and Departments) that loads all of the departments given a College code, I get an error that "colleges is not associated to departments".
From the documentation on eager load, I've gathered that, although I relate the schemas by defining the foreign key in Departments, I must establish the relation by creating an association (which ultimately creates a foreign key for me) instead of creating the foreign key in the schema myself. eg.
Colleges.hasMany(Departments, { foreignKey: "collegeId" });
However, this does not allow me to make the foreign key apart of the composite unique key as I do in the schema.
How can I tackle this problem?
asked 1 min ago
Eager load on foreign key apart of composite unique key
Aucun commentaire:
Enregistrer un commentaire