最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Sequelize: "Model has not been defined" when relationship across multiple databases - Stack Overf

programmeradmin1浏览0评论

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:

  • SequelizeJS: How to include association (join) across multiple databases without using raw query

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:

  • SequelizeJS: How to include association (join) across multiple databases without using raw query
Share Improve this question edited Feb 6, 2020 at 10:12 Gaetan Le Gac asked Feb 6, 2020 at 7:54 Gaetan Le GacGaetan Le Gac 1412 silver badges5 bronze badges 2
  • Hi, I'm having the exact same problem, were you able to solve it? – peter_b Commented Mar 31, 2021 at 21:09
  • Hi, because Sequelize did not meet my needs in many ways, I ended up by writting my own orm. – Gaetan Le Gac Commented Apr 2, 2021 at 5:35
Add a ment  | 

2 Answers 2

Reset to default 6

Please make sure you define the entity on sequlize object.

export const databaseProviders = [
  {
    provide: 'SEQUELIZE',
    useFactory: async () => {
      const sequelize = new Sequelize({
        dialect: 'mysql',
        host: 'localhost',
        port: ------,
        username: '-----',
        password: '----',
        database: '----',
      });
      sequelize.addModels([Contenu, OtherEntityHere]);
      await sequelize.sync( {alter: true});
      return sequelize;
    },
  },
];

or else sequlize wont know the entity and its telling you that its not defined too.

Sequalize doesn't support multiple migration folders or distinguish between the methods you set for individual models. You have to be really specific. I can't tell if you are from what you've provided.

See if this SO question helps: Migrations in Sequelize in my own folder structure You have to configure a way around the default path.

This may offer some insight, too: https://medium./unetiq/using-multiple-databases-with-nodejs-and-sequelize-59e0abcbbe6f

You can read about it Sequalize's specifics on migration here: https://sequelize/master/manual/migrations.html#the-sequelizerc-file

发布评论

评论列表(0)

  1. 暂无评论