[eggjs/egg]使用egg-Sequelize model添加associate属性报错

2024-03-29 932 views
2
What happens?

仿照官方demo,但是报错

Mini Showcase Repository(REQUIRED)

  const NewModel = app.model.define("new", {
    id: { type: INTEGER, primaryKey: true, autoIncrement: true },
    name: STRING(24),
    sort: INTEGER(),
    des: TEXT(),
    main_img: TEXT(),
    main_content: TEXT(),
    status: INTEGER(),
    created_at: DATE,
    updated_at: DATE,
  });

  NewModel.associate = function () {
    app.model.NewModel.belongsToMany(app.model.newsCategory, {
      as: "Tasks",
      through: app.model.categoryForNewModel,
      foreignKey: "userId",
    });
  };

error info

TSError: [egg-core] load file: /Users/skylee/wordspace/merculet-official-website-backend/app/model/news.ts, error: ⨯ Unable to compile TypeScript:
app/model/news.ts(18,12): error TS2339: Property 'associate' does not exist on type 'typeof Model'.

    at createTSError (/Users/skylee/wordspace/merculet-official-website-backend/node_modules/ts-node/src/index.ts:261:12)
    at getOutput (/Users/skylee/wordspace/merculet-official-website-backend/node_modules/ts-node/src/index.ts:367:40)
    at Object.compile (/Users/skylee/wordspace/merculet-official-website-backend/node_modules/ts-node/src/index.ts:558:11)
    at Module.m._compile (/Users/skylee/wordspace/merculet-official-website-backend/node_modules/ts-node/src/index.ts:439:43)
    at Module._extensions..js (internal/modules/cjs/loader.js:1153:10)
    at Object.require.extensions.<computed> [as .ts] (/Users/skylee/wordspace/merculet-official-website-backend/node_modules/ts-node/src/index.ts:442:12)
    at Module.load (internal/modules/cjs/loader.js:977:32)
    at Function.Module._load (internal/modules/cjs/loader.js:877:14)
    at Module.require (internal/modules/cjs/loader.js:1019:19)
    at require (internal/modules/cjs/helpers.js:77:18)
    at Object.loadFile (/Users/skylee/wordspace/merculet-official-website-backend/node_modules/egg-core/lib/utils/index.js:27:19)
    at getExports (/Users/skylee/wordspace/merculet-official-website-backend/node_modules/egg-core/lib/loader/file_loader.js:199:23)
    at FileLoader.parse (/Users/skylee/wordspace/merculet-official-website-backend/node_modules/egg-core/lib/loader/file_loader.js:158:25)
    at FileLoader.load (/Users/skylee/wordspace/merculet-official-website-backend/node_modules/egg-core/lib/loader/file_loader.js:67:24)
    at AppWorkerLoader.loadToApp (/Users/skylee/wordspace/merculet-official-website-backend/node_modules/egg-core/lib/loader/egg_loader.js:388:25)
    at loadDatabase (/Users/skylee/wordspace/merculet-official-website-backend/node_modules/egg-sequelize/lib/loader.js:101:16)
    at module.exports (/Users/skylee/wordspace/merculet-official-website-backend/node_modules/egg-sequelize/lib/loader.js:32:20)
    at module.exports (/Users/skylee/wordspace/merculet-official-website-backend/node_modules/egg-sequelize/app.js:4:26)
    at Hook.configDidLoad (/Users/skylee/wordspace/merculet-official-website-backend/node_modules/egg-core/lib/lifecycle.js:91:9)
    at Lifecycle.triggerConfigDidLoad (/Users/skylee/wordspace/merculet-official-website-backend/node_modules/egg-core/lib/lifecycle.js:145:14)
    at Lifecycle.triggerConfigWillLoad (/Users/skylee/wordspace/merculet-official-website-backend/node_modules/egg-core/lib/lifecycle.js:139:10)
    at AppWorkerLoader.loadCustomApp (/Users/skylee/wordspace/merculet-official-website-backend/node_modules/egg-core/lib/loader/mixin/custom.js:37:20)
How To Reproduce

Steps to reproduce the behavior: 1. 2.

Expected behavior 1. 2.

Context
  • Node Version: v12.16.3

  • Egg Version: "egg": "^2.6.1",

  • Plugin Name: "egg-scripts": "^2.6.0", "egg-sequelize": "^5.2.2",

回答

7

egg-sequelize 是官方维护的插件,理论上都是经过严格测试了,是没有问题的。 你应该给出你的最小可复现仓库,现在给的这些信息,是没办法排查问题的。

4

有一个投机取巧的方法

return class extends NewModel {
    static associate() {
      NewModel.belongsToMany(app.model.newsCategory, {
        as: "Tasks",
        through: app.model.categoryForNewModel,
        foreignKey: "userId",
      });
    }
  };
5

所以在ts上面Model. associate报错的问题还没解决? 类型“ModelCtor<Model<any, any>>”上不存在属性“associate”

6

我现在也是碰到这个问题,看来还是没解决。。。

3

这个也只能用在 egg-sequelize 5.0 版本,升到 6.0 又不行了

3

可以自己修复一下,添加一个index.d.ts文件

import "egg";
import * as sequelize from "sequelize";

declare module "sequelize" {
  namespace Model {
    // 修复 model.associate 错误
    export function associate() {}
  }
  module DataTypes {
    // 修复 INTEGER(x: number) 错误
    interface IntegerDataTypeConstructor extends sequelize.DataTypes.NumberDataTypeConstructor {
      new (options?: number): sequelize.DataTypes.IntegerDataType;
      (options?: number): sequelize.DataTypes.IntegerDataType;
    }
  }
}

declare module "egg" {
  interface IModel {
    // 覆盖 define 使用扩展后的 sequelize
    public define<M extends sequelize.Model, TAttributes = sequelize.Attributes<M>>(
      modelName: string,
      attributes: sequelize.ModelAttributes<M, TAttributes>,
      options?: sequelize.ModelOptions<M>
    ): sequelize.ModelCtor<M>;
  }
  interface Application {
    // 覆盖 Sequelize 使用扩展后的 sequelize
    Sequelize: typeof sequelize;
  }
}