0

i need to map multiple entities using sequelize, and i just don't understand where the error lies. I keep getting " Error: Profesor.hasOne called with something that's not a subclass of Sequelize.Model". How do i fix this ?

Professor model class:

import { Association, NonAttribute, CreationOptional, DatabaseError, DataTypes, InferAttributes, InferCreationAttributes, Model, Optional, Sequelize, HasOneGetAssociationMixin, HasOneCreateAssociationMixin, HasOneSetAssociationMixin, BelongsToGetAssociationMixin, BelongsToSetAssociationMixin, BelongsToCreateAssociationMixin } from 'sequelize'
import sequelize from '../config'
import Clasa from './Clasa';

class Profesor extends Model<InferAttributes<Profesor>, InferCreationAttributes<Profesor>>{
  declare id: CreationOptional<number>;
  declare nume: string;
  declare prenume: string;
  declare CNP: string;
  declare password: string;
  declare id_clasa_dirigentie: CreationOptional<number>;

  declare getClasa: HasOneGetAssociationMixin<Clasa>;
  declare setClasa: HasOneSetAssociationMixin<Clasa, 'id'>;
  declare addClasa: HasOneCreateAssociationMixin<Clasa>;

  declare clasa?: NonAttribute<Clasa>;

  declare static associations: {
    clase: Association<Profesor, Clasa>;
  };
}

Profesor.init(
  {
    id: {
      type: DataTypes.BIGINT,
      autoIncrement: true,
      primaryKey: true
    },
    nume: {
        type: new DataTypes.STRING,
        allowNull: false
    },
    prenume: {
        type: new DataTypes.STRING,
        allowNull: false
    },
    CNP: {
        type: new DataTypes.STRING,
        allowNull: false
    },
    password: {
        type: new DataTypes.STRING,
        allowNull: false
    },
    id_clasa_dirigentie: {
        type: new DataTypes.BIGINT,
    }
},
  {
    tableName: 'profesor',
    sequelize
  }
);
Profesor.hasOne(Clasa, { sourceKey: 'id' });

(async () => {
  await sequelize.sync();
})();

export default Profesor;

Clasa model class:

import { BelongsToCreateAssociationMixin, BelongsToGetAssociationMixin, BelongsToManyCreateAssociationMixin, BelongsToManyGetAssociationsMixin, BelongsToManySetAssociationsMixin, BelongsToSetAssociationMixin, CharDataType, CreationOptional, DatabaseError, DataTypes, InferAttributes, InferCreationAttributes, Model, Optional, Sequelize, SmallIntegerDataType } from 'sequelize'
import sequelize from '../config'
import Materie from './Materie';
import Profesor from './Profesor';
import Student from './Student';

class Clasa extends Model<InferAttributes<Clasa>, InferCreationAttributes<Clasa>>{
  declare id: CreationOptional<number>;
  declare idDiriginte: bigint;
  declare an: SmallIntegerDataType;
  declare litera: CharDataType;

  declare getStudent: BelongsToGetAssociationMixin<Student>;
  declare setStudent: BelongsToSetAssociationMixin<Student, 'id'>;
  declare addStudent: BelongsToCreateAssociationMixin<Student>;

  declare getDiriginte: BelongsToGetAssociationMixin<Profesor>;
  declare setDiriginte: BelongsToSetAssociationMixin<Profesor, 'id'>;
  declare addDiriginte: BelongsToCreateAssociationMixin<Profesor>;

  declare getMaterie: BelongsToManyGetAssociationsMixin<Materie>;
  declare setMaterie: BelongsToManySetAssociationsMixin<Materie,'id'>;
  declare addMaterie: BelongsToManyCreateAssociationMixin<Materie>;
  
  declare get
}

Clasa.init(
  {
    id: {
      type: DataTypes.BIGINT,
      autoIncrement: true,
      primaryKey: true
    },
    idDiriginte: {
        type: new DataTypes.BIGINT
    },
    an: {
        type: new DataTypes.SMALLINT
    },
    litera: {
        type: new DataTypes.CHAR
    }
},
  {
    tableName: 'clasa',
    sequelize
  }
);
Clasa.belongsTo(Student, {foreignKey: 'id_clasa'});
Clasa.belongsTo(Profesor, {foreignKey: 'id_clasa_dirigentie'});
Clasa.belongsToMany(Materie,{through: 'profesor_clasa', foreignKey:'id_clasa'});
Clasa.belongsToMany(Profesor,{through: 'profesor_clasa', foreignKey:'id_profesor'});

export default Clasa;

index file:

import 'dotenv/config'
import express, {Request,Response,Application} from 'express';
import testConnection from './src/db/config';
import { Sequelize } from 'sequelize';
import Student from './src/db/models/Student';
import Materie from './src/db/models/Materie';
import Profesor from './src/db/models/Profesor';
import Clasa from './src/db/models/Clasa';

require('dotenv').config;

const app:Application = express();
const PORT = process.env.PORT || 8000;

app.get("/", (req:Request, res:Response):void => {
   res.send("Hello Typescript with Node.jsx!")
  async () => {
    const project = await Clasa.findByPk(1);
    if (project === null) {
      console.log('Not found!');
    } else {
      console.log(project instanceof Clasa); // true
}
  }
 });

  app.listen(PORT, ():void => {
    console.log(`Server Running here: https://localhost:${PORT}`);
  });

I've looked for solutions online and they said that putting them together would solve the problem, but that didn't seem to work in my case

Vasile Mihai
  • 103
  • 6

1 Answers1

0

You need to register associations separately after you initialize all models, see my answer to get an idea of how to do it.

Anatoly
  • 20,799
  • 3
  • 28
  • 42