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')
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/545246.html
標籤:其他
