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

I'm just a beginner, I got this error when I set up the association table, I don't know why

Open cuilonglong0422 opened this issue 2 years ago • 1 comments

association.init is not a function image image image image I hope you can help me see it when you have time, I don't want to use the basic methods in the sequelize documentation, it will look very unprofessional

cuilonglong0422 avatar Oct 19 '22 14:10 cuilonglong0422

this is my source code // class user

 * @description:
 * @author: cuicui
 * @Date: 2022-09-15 23:20:49
 * @LastEditTime: 2022-10-19 21:45:49
 * @LastEditors: cuicui
 */
import { Idate } from './common';
import { Column, IsUUID,
  HasMany,
  DataType, Model, PrimaryKey, Table, AllowNull, Default, Index, DeletedAt, Scopes } from 'sequelize-typescript';
import BankCard from './bankCard';
interface IUser extends Idate {
  Id:string
  username: string
  password: string
  sex: number
  avatar: string
  city: string
  enabled: number
  accountBalance: number
}
@Scopes(() => ({
  bankCard: {
    include: [{
      model: BankCard,
      through: { attributes: [] },
    }],
  },
}))
@Table
export default class User extends Model<IUser> {
  @IsUUID(4)
  @PrimaryKey
  @AllowNull(false)
  @Default('')
  @Column
    Id: string;

  @Index
  @AllowNull(false)
  @Column({
    comment: '账号',
  })
    username: string;

  @AllowNull(false)
  @Column({
    comment: '姓名',
  })
    realName: string;

  @AllowNull(false)
  @Column({
    type: DataType.STRING,
    comment: '密码',
  })
    password: string;

  @Column({
    type: DataType.INTEGER,
    comment: '性别',
  })
    sex: number;

  @Column({
    type: DataType.STRING,
    comment: '头像',
  })
    avatar: string;

  @Column({
    type: DataType.STRING,
    comment: '城市',
  })
    city: string;

  @AllowNull(false)
  @Default(0)
  @Column({
    type: DataType.INTEGER,
    comment: '启用状态: 0启用,1禁用',
  })
    enabled: number;

  @Column({
    type: DataType.DECIMAL,
    comment: '账户余额',
  })
    accountBalance: number;

  @Column({
    type: DataType.DATE,
  })
    created_at: string;

  @Column({
    type: DataType.DATE,
  })
    updated_at: string;

  @DeletedAt
  @Column({
    type: DataType.DATE,
  })
    deleted_at: string;


  @HasMany(() => BankCard, 'userId')
  bankCard?: BankCard[];
}

// class bankcard

/*
 * @description:
 * @author: cuicui
 * @Date: 2022-10-15 17:35:17
 * @LastEditTime: 2022-10-19 20:43:25
 * @LastEditors: cuicui
 */
import { Idate } from './common';
// import Bank from './bank';
import User from './user';
import { Column, IsUUID,
  ForeignKey,
  //  BelongsTo,
  DataType, Model, PrimaryKey, Table, AllowNull, Default, Index, DeletedAt, Scopes } from 'sequelize-typescript';
interface IBankCard extends Idate {
  Id:string
  cardId: string
  bankId: string
  userId: string
}
@Scopes(() => ({
  user: {
    include: [
      {
        model: User,
        through: { attributes: [] },
      },
    ],
  },
}))
@Table
class BankCard extends Model<IBankCard> {
  @IsUUID(4)
  @PrimaryKey
  @Index
  @AllowNull(false)
  @Default('')
  @Column
    Id: string;

  @Index('userId_bankId_cardId')
  @AllowNull(false)
  @Column({
    comment: '银行卡号',
  })
    cardId: string;

  @Index('userId_bankId_cardId')
  //   @ForeignKey(() => Bank)
  @AllowNull(false)
  @Column({
    comment: '所属银行',
  })
    bankId: string;

  @Index('userId_bankId_cardId')
  @AllowNull(false)
  @ForeignKey(() => User)
  @Column({
    comment: '所属用户',
  })
    userId: string;

  // @BelongsTo(() => User, 'userId')
  //   user: User;

  @Column({
    type: DataType.DATE,
  })
    created_at: string;

  @Column({
    type: DataType.DATE,
  })
    updated_at: string;

  @DeletedAt
  @Column({
    type: DataType.DATE,
  })
    deleted_at: string;

//   @BelongsTo(() => Bank, 'bankId')
//     bank: Bank;
}

export default BankCard;

// packge.json

{
	"name": "rear-end",
	"version": "1.0.0",
	"description": "",
	"private": true,
	"egg": {
		"typescript": true,
		"declarations": true
	},
	"scripts": {
		"start": "egg-scripts start --daemon --title=egg-server-rearEnd",
		"stop": "egg-scripts stop --title=egg-server-rearEnd",
		"dev": "egg-bin dev --port 9527",
		"debug": "egg-bin debug",
		"test-local": "egg-bin test",
		"test": "npm run lint -- --fix && npm run test-local",
		"cov": "egg-bin cov",
		"tsc": "ets && tsc -p tsconfig.json",
		"ci": "npm run lint && npm run cov && npm run tsc",
		"autod": "autod",
		"lint": "eslint . --ext .ts --resolve-plugins-relative-to .",
		"clean": "ets clean"
	},
	"dependencies": {
		"@types/sequelize": "^4.28.14",
		"await-stream-ready": "^1.0.1",
		"cryptojs": "^2.5.3",
		"egg": "^2.6.1",
		"egg-cors": "^2.2.3",
		"egg-jwt": "^3.1.7",
		"egg-redis": "^2.4.0",
		"egg-scripts": "^2.6.0",
		"egg-sequelize": "5.2.2",
		"egg-sequelize-ts": "0.2.3-0",
		"egg-static": "^2.2.0",
		"egg-swagger-doc": "^2.3.2",
		"egg-valparams": "^1.4.5",
		"mysql": "^2.18.1",
		"mysql2": "^2.3.3",
		"reflect-metadata": "^0.1.13",
		"sequelize": "5.12.2",
		"sequelize-cli": "^6.4.1",
		"sequelize-cli-typescript": "3.2.0-c",
		"sequelize-typescript": "^1.1.0",
		"stream-wormhole": "^1.1.0",
		"uuid": "^9.0.0"
	},
	"devDependencies": {
		"@eggjs/tsconfig": "^1.0.0",
		"@types/bluebird": "^3.5.36",
		"@types/mocha": "^2.2.40",
		"@types/node": "^7.10.14",
		"@types/supertest": "^2.0.0",
		"@types/validator": "^13.7.6",
		"autod": "^3.0.1",
		"autod-egg": "^1.1.0",
		"egg-bin": "^4.11.0",
		"egg-ci": "^2.1.0",
		"egg-mock": "^3.16.0",
		"eslint": "^8.0.0",
		"eslint-config-egg": "^12.0.0",
		"typescript": "^4.0.0"
	},
	"engines": {
		"node": ">=14.0.0"
	},
	"ci": {
		"version": "14, 16, 18"
	},
	"repository": {
		"type": "git",
		"url": ""
	},
	"eslintIgnore": [
		"coverage"
	],
	"author": "",
	"license": "MIT"
}

cuilonglong0422 avatar Oct 19 '22 14:10 cuilonglong0422