侧边栏壁纸
  • 累计撰写 17 篇文章
  • 累计创建 7 个标签
  • 累计收到 2 条评论

目 录CONTENT

文章目录

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

抗争的小青年
2023-02-27 / 0 评论 / 0 点赞 / 717 阅读 / 424 字
温馨提示:
感谢您的阅读,若在文中留有不当引用,请及时联系管理

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')
0

评论区