A煤n no tienes acceso a esta clase

Crea una cuenta y contin煤a viendo este curso

Modificando una entidad

17/33
Recursos

Aportes 4

Preguntas 1

Ordenar por:

驴Quieres ver m谩s aportes, preguntas y respuestas de la comunidad? Crea una cuenta o inicia sesi贸n.

Lo que vimos:

product.entity.ts

import {
  PrimaryGeneratedColumn,
  Column,
  Entity,
  CreateDateColumn,
  UpdateDateColumn,
} from 'typeorm';

@Entity()
export class Product {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ type: 'varchar', length: 255, unique: true })
  name: string;

  @Column({ type: 'text' })
  description: string;

  @Column({ type: 'int' })
  price: number;

  @Column({ type: 'int' })
  stock: number;

  @Column({ type: 'varchar' })
  image: string;

  @CreateDateColumn({
    type: 'timestamptz',
    default: () => 'CURRENT_TIMESTAMP',
  })
  createAt: Date;

  @UpdateDateColumn({
    type: 'timestamptz',
    default: () => 'CURRENT_TIMESTAMP',
  })
  updateAt: Date;
}

Al agregar cambios a una entidad, se tiene que generar la migracion y ejecutarla.

Se le agrega a la entidad product las fechas de creacion y actualizacion automaticas:

import {
  PrimaryGeneratedColumn,
  Column,
  Entity,
  CreateDateColumn,
  UpdateDateColumn,
} from 'typeorm';

@Entity()
export class Product {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ type: 'varchar', length: 255, unique: true })
  name: string;

  @Column({ type: 'text' })
  description: string;

  @Column({ type: 'int' })
  price: number;

  @Column({ type: 'int' })
  stock: number;

  @Column({ type: 'varchar' })
  image: string;

  @CreateDateColumn({
    type: 'timestamptz',
    default: () => 'CURRENT_TIMESTAMP',
  })
  createAt: Date;

  @UpdateDateColumn({
    type: 'timestamptz',
    default: () => 'CURRENT_TIMESTAMP',
  })
  updateAt: Date;

Se genera la migracion npm run migrations:generate -- add-fields

Migration /home/wlensinas/code/nestjs-typeorm/src/database/migrations/1621891104050-add-fields.ts has been generated successfully.

Se ejecutan los cambios en la base de datos npm run migrations:run

query: SELECT * FROM "information_schema"."tables" WHERE "table_schema" = current_schema() AND "table_name" = 'migrations'
query: SELECT * FROM "migrations" "migrations" ORDER BY "id" DESC
1 migrations are already loaded in the database.
2 migrations were found in the source code.
init1621883836551 is the last executed migration. It was executed on Mon May 24 2021 16:17:16 GMT-0300 (Argentina Standard Time).
1 migrations are new migrations that needs to be executed.
query: START TRANSACTION
query: ALTER TABLE "product" ADD "createAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP
query: ALTER TABLE "product" ADD "updateAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT CURRENT_TIMESTAMP
query: INSERT INTO "migrations"("timestamp", "name") VALUES ($1, $2) -- PARAMETERS: [1621891104050,"addFields1621891104050"]
Migration addFields1621891104050 has been executed successfully.
query: COMMIT

Se listan los cambios npm run migrations:show

query: SELECT * FROM "information_schema"."tables" WHERE "table_schema" = current_schema() AND "table_name" = 'migrations'
query: SELECT * FROM "migrations" "migrations" ORDER BY "id" DESC
 [X] init1621883836551
 [X] addFields1621891104050
[ExceptionHandler] No repository for "Brand" was found. Looks like this entity is not registered in current 
"default" connection? +184ms

Por si les sale este error, recuerden agregar en el archivo database.module.ts la propiedad de autoLoadEntities.

En mi caso que estoy usando mysql solo me funciono de esta forma

@Column({ type: "timestamp", default: () => "CURRENT_TIMESTAMP"})
  createAt: Date;

@Column({ type: "timestamp", default: () => "CURRENT_TIMESTAMP"})
  updateAt: Date;