0

My user.model.ts is:

@Table
export class User extends Model<User> {
    @Column({ allowNull: false })
    firstName: string;

    @Column({ allowNull: false })
    lastName: string;
...

When I try to import it:

    const sequelize = new Sequelize(DB_CREDENTIALS_PARSED.database, DB_CREDENTIALS_PARSED.username, DB_CREDENTIALS_PARSED.password, { dialect: 'postgres' })
    const modelFile = require(path.join(modelPath, 'user.model')).User.init({ sequelize })

    console.log(modelFile)

I get an error: Error: No Sequelize instance passed

What am I doing wrong?

1 Answer 1

1

The sequelize instance must be passed in the second argument of the init method

const User = require(path.join(modelPath, 'user.model'));
const sequelize = new Sequelize(
    DB_CREDENTIALS_PARSED.database,
    DB_CREDENTIALS_PARSED.username,
    DB_CREDENTIALS_PARSED.password, {
        dialect: 'postgres'
    }
)
User.init(modelAttributesObject, { sequelize })

But as you are using the sequelize TypeScript, you can initialize your models with the instance

const User = require(path.join(modelPath, 'user.model'));
const sequelize = sequelize = new Sequelize({
    dialect: 'postgres',
    database: DB_CREDENTIALS_PARSED.database,
    username: DB_CREDENTIALS_PARSED.username,
    password: DB_CREDENTIALS_PARSED.database,
    models: [User]
});
Sign up to request clarification or add additional context in comments.

8 Comments

But isn't modelAttributesObject coming from my definition?
Yes it is, that is why you don't need the ìnit` method here as it require the attributes to be passed as first argument. Either you use the TypeScript version and passe models to sequelize instance or the Javascript version and use the init method
With the Typescript version, what about relationships?
"Relations can be described directly in the model" from the sequelize-typescript. See npmjs.com/package/sequelize-typescript#model-association
So as long as I include all the models, everything should just work?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.