typescript-sequelize-example icon indicating copy to clipboard operation
typescript-sequelize-example copied to clipboard

Sequelize v5 types problem

Open francessco410 opened this issue 6 years ago • 3 comments

Hello,

I was following yours great tutorial but I tried to set up my project using Sequelize v5, instead of v4, however, I got many errors regarding types incompatibility. Please check my code below.

import * as Sequelize from 'sequelize';

export interface UserAttributes {
    id?: number;
    name: string;
    createdAt?: Date;
    updatedAt?: Date;
}

export interface UserInstance extends Sequelize.Instance<UserAttributes>, UserAttributes {}

Error: TS2694: Namespace "... /node_modules/sequelize/types/index" has no exported member "Instance"

Similar issue on StackOverflow:

  • https://stackoverflow.com/questions/56752749/using-sequelize-instance-with-sequelize-v5-typescript

I would be grateful if you could help me solve this problem.

francessco410 avatar Sep 29 '19 18:09 francessco410

Same here. Do you have any solution or workaround for this problem?

leduclinh7141 avatar Oct 23 '19 05:10 leduclinh7141

+1

calarasmihail avatar Nov 17 '19 20:11 calarasmihail

Hi. I did not find a way to fix this, but I did find a workaround for ManyToMany relationships: I delete the join table created by sequelize and recreated it using: CREATE TABLE IF NOT EXISTS "Students_Classes" ("ClassClassId" INTEGER REFERENCES "Classes" ("class_id") ON DELETE CASCADE ON UPDATE CASCADE, "UserUserId" INTEGER REFERENCES "Users" ("user_id") ON DELETE CASCADE ON UPDATE CASCADE, "id" SERIAL UNIQUE, PRIMARY KEY ("id"));

Instead of declaring methods, I just made the new table and filled it manually.

this was my model:

import {
	Model,
	Sequelize,
    INTEGER
} from "sequelize"

class Students_Class extends Model {
    id!: number
	ClassClassId!: number
	UserUserId!: number

	static initialize(sequelize: Sequelize) {
		this.init(
            {
                id: {
					allowNull: false,
					autoIncrement: true,
					primaryKey: true,
					type: INTEGER,
					unique: true,
				},
                ClassClassId: {
                    allowNull: false,
                    type: INTEGER
                },
                UserUserId: {
                    allowNull: false,
                    type: INTEGER
                }
            },
			{
				sequelize,
                timestamps: false,
				modelName: "Students_Class",
			}
		)
	}
}


export default Students_Class`

EDIT: please keep in mind this was my first typescript project using sequelize. Feel free to correct my code if you have found a new way of doing this.

LidiaKovac avatar Mar 24 '21 11:03 LidiaKovac