创建自动生成的列

假设你希望 id 列自动生成(这称为 auto-increment/sequence/serial/generated identity column)。为此你需要将@PrimaryColumn装饰器更改为@PrimaryGeneratedColumn装饰器:

  1. import { Entity, Column, PrimaryGeneratedColumn } from "typeorm";
  2. @Entity()
  3. export class Photo {
  4. @PrimaryGeneratedColumn()
  5. id: number;
  6. @Column()
  7. name: string;
  8. @Column()
  9. description: string;
  10. @Column()
  11. filename: string;
  12. @Column()
  13. views: number;
  14. @Column()
  15. isPublished: boolean;
  16. }