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