sequelize-auto
sequelize-auto copied to clipboard
Typescript error: Generated models cannot be used where Sequelize expects a "Model"
sequelize 6.3.5, sequelize-auto 0.8.3 The biggest issue where this happens is "through" associations that get generated in init-models.
init-models.ts
external_company.belongsToMany(job_auction, {
as: 'job_auction_id_job_auctions',
through: job_auction_designated,
foreignKey: 'company_id',
otherKey: 'job_auction_id',
});
Error on "through" attribute:
Type 'typeof job_auction_designated' is not assignable to type 'string | typeof Model | ThroughOptions'.
Types of construct signatures are incompatible.
Type 'new (values?: job_auction_designatedCreationAttributes | undefined, options?: BuildOptions | undefined) => job_auction_designated' is not assignable to type 'abstract new <TModelAttributes extends {} = any, TCreationAttributes extends {} = TModelAttributes>(values?: TCreationAttributes | undefined, options?: BuildOptions | undefined) => Model<TModelAttributes, TCreationAttributes>'.
Construct signature return types 'job_auction_designated' and 'Model<TModelAttributes, TCreationAttributes>' are incompatible.
The types of '_attributes' are incompatible between these types.
Type 'job_auction_designatedAttributes' is not assignable to type 'TModelAttributes'.
'job_auction_designatedAttributes' is assignable to the constraint of type 'TModelAttributes', but 'TModelAttributes' could be instantiated with a different subtype of constraint '{}'.
For reference, here's part of the generated model in question
export class job_auction_designated extends Model<job_auction_designatedAttributes, job_auction_designatedCreationAttributes> implements job_auction_designatedAttributes {
company_id!: string;
date_create?: Date;
job_auction_id!: number;
// job_auction_designated belongsTo external_company via company_id
company!: external_company;
getCompany!: Sequelize.BelongsToGetAssociationMixin<external_company>;
setCompany!: Sequelize.BelongsToSetAssociationMixin<external_company, external_companyId>;
createCompany!: Sequelize.BelongsToCreateAssociationMixin<external_company>;
// job_auction_designated belongsTo job_auction via job_auction_id
job_auction!: job_auction;
getJob_auction!: Sequelize.BelongsToGetAssociationMixin<job_auction>;
setJob_auction!: Sequelize.BelongsToSetAssociationMixin<job_auction, job_auctionId>;
createJob_auction!: Sequelize.BelongsToCreateAssociationMixin<job_auction>;
static initModel(sequelize: Sequelize.Sequelize): typeof job_auction_designated {
...
Pretty much the same error happens when using any generated model as the "model" parameter of an include. Example:
brand.getBoards({include: {model: board}})
Error
Type '{ model: typeof board; }' is not assignable to type 'Includeable | Includeable[] | undefined'.
Types of property 'model' are incompatible.
Type 'typeof board' is not assignable to type 'typeof Model'.
Types of construct signatures are incompatible.
Type 'new (values?: boardCreationAttributes | undefined, options?: BuildOptions | undefined) => board' is not assignable to type 'abstract new <TModelAttributes extends {} = any, TCreationAttributes extends {} = TModelAttributes>(values?: TCreationAttributes | undefined, options?: BuildOptions | undefined) => Model<TModelAttributes, TCreationAttributes>'.
Types of parameters 'values' and 'values' are incompatible.
Type 'TCreationAttributes | undefined' is not assignable to type 'boardCreationAttributes | undefined'.
Type 'TCreationAttributes' is not assignable to type 'boardCreationAttributes | undefined'.
Type '{}' is not assignable to type 'boardCreationAttributes'.
Type '{}' is not assignable to type 'Omit<boardAttributes, "board_id">'.
Type 'TCreationAttributes' is not assignable to type 'boardCreationAttributes'.
Type '{}' is not assignable to type 'boardCreationAttributes'.
Type '{}' is not assignable to type 'Omit<boardAttributes, "board_id">'.
Type 'TCreationAttributes' is not assignable to type 'Omit<boardAttributes, "board_id">'.
Type '{}' is missing the following properties from type 'Omit<boardAttributes, "board_id">':
Thanks for finding this. Do you have an idea for the fix? What should the sequelize-auto output be for your model?
One work around is to use the model name (string) instead:
through: 'job_auction_designated',