I'm trying to instanciate one sequelize per Database. Some tables in DB1 has relationships with tables in DB2, and reverse. One of the relationships is DB1.Utilisateur.contenuProvenance_id => DB2.Contenu.id and DB2.Contenu.utilisateur_id => DB1.Utilisateur.id.
Here is the Utilisateur model:
@Table
export default class Utilisateur extends Model<Utilisateur> {
@API() @PrimaryKey @AutoIncrement @Column({ type: DataType.INTEGER })
public id!: number;
@Column({ type: DataType.STRING, allowNull: false })
public email!: string;
@Column({ type: DataType.STRING, allowNull: false })
public nom!: string;
@ForeignKey(() => Utilisateur)
@Column({ type: DataType.INTEGER, allowNull: true })
public parrain_id?: number;
@ForeignKey(() => Contenu)
@Column({ type: DataType.INTEGER, allowNull: true })
public contenuProvenance_id?: number;
}
(Yes i'm using sequelize-typescript to declare my models as Typescript classes)
And here is how my Sequelize instance are created:
let DBs = {};
for (const ZONE of zones)
DBs[ ZONE.name ] = new Sequelize(ZONE.serveur.bdd.nom, ZONE.serveur.bdd.login, ZONE.serveur.bdd.mdp, {
host: ZONE.serveur.bdd.hote,
dialect: "mariadb",
...
models: ZONE.modeles // Array of models
});
The problem: when the first instance (DB1) is created, sequelize don't find the Content table and gives me this error:
Error: Contenu has not been defined
at Sequelize.model (project/node_modules/sequelize/lib/sequelize.js:443:13)
at Sequelize.model (project/node_modules/sequelize-typescript/dist/sequelize/sequelize/sequelize.js:29:26)
at project/node_modules/sequelize-typescript/dist/sequelize/sequelize/sequelize.js:51:46
at Array.forEach (<anonymous>)
at project/node_modules/sequelize-typescript/dist/sequelize/sequelize/sequelize.js:49:26
at Array.forEach (<anonymous>)
at Sequelize.associateModels (project/node_modules/sequelize-typescript/dist/sequelize/sequelize/sequelize.js:45:16)
at Sequelize.addModels (project/node_modules/sequelize-typescript/dist/sequelize/sequelize/sequelize.js:37:14)
at new Sequelize (project/node_modules/sequelize-typescript/dist/sequelize/sequelize/sequelize.js:19:22)
Is it possible to create the sequelize instances one by one, although there are relations between databases ?
Should I add models after all sequelize instances has been created ?
Thanks for your help guys
Related:
from Sequelize: "Model has not been defined" when relationship across multiple databases
No comments:
Post a Comment