发布时间:2023-02-28 文章分类:编程知识 投稿人:赵颖 字号: 默认 | | 超大 打印

nestJs中使用typeORM报'QueryFailedError: Table 'equtype' already exists'错误

nestJs中使用typeORM报'QueryFailedError: Table 'equtype' already exists'错误
如图,博主在定义实体类的时候,代码如下

import { Entity, Column, PrimaryGeneratedColumn, CreateDateColumn } from 'typeorm';
import { ApiProperty } from '@nestjs/swagger';
/**
 * 健身器材类型实体
 */
@Entity('equType')
export class Equtype {
  @ApiProperty({ description: 'id' })
  @PrimaryGeneratedColumn('uuid')
  id: string;
  @ApiProperty({ description: '类型名称' })
  @Column({ length: 255 })
  name: string;
  @ApiProperty({ description: '类型描述' })
  @Column({ length: 999 })
  scr: string;
  @ApiProperty({ description: '状态:0-未启用,1-启用' })
  @Column({ type: "enum", enum: [0, 1], default: 1 })
  status: number;
  @CreateDateColumn()
  add_time: Date
}

并将该实体注入到DataSource的entity中:

export const AppDataSource = new DataSource({
  type: 'mysql',
  host: 'localhost',
  port: 3306,
  username: 'root',
  password: 'root',
  database: 'lybs-jdweb',
  // charset: 'utf8mb4',
  timezone: '+08:00',
  synchronize: true, // 是否同步,如果为true,新建的实体会更新建表或更新字段
  logging: false, // 是否开启日志,为true 为打印执行的sql
  entities: [Admin, Role, Buildequ, Equtype], // 数据表实体
});

并重启项目,这时就会存在如上图所示的错误,我画了半个小时解决了该问题:

后来我发现我在定义实体的时候

import { Entity, Column, PrimaryGeneratedColumn, CreateDateColumn } from 'typeorm';
import { ApiProperty } from '@nestjs/swagger';
@entity('equType')
export class Equtype {
@ApiProperty({ description: 'id' })
@PrimaryGeneratedColumn('uuid')
id: string;
@ApiProperty({ description: '类型名称' })
@column({ length: 255 })
name: string;
@ApiProperty({ description: '类型描述' })
@column({ length: 999 })
scr: string;
@ApiProperty({ description: '状态:0-未启用,1-启用' })
@column({ type: "enum", enum: [0, 1], default: 1 })
status: number;
@CreateDateColumn()
add_time: Date
}

@entity里注册使用了驼峰命名,我后来将其改成小写就解决了该问题,希望对你有所帮助!

before:
@Entity('equType')
after:
@Entity('equtype')