Ayantunji Timilehin
Ayantunji Timilehin

Reputation: 243

Running migration in NestJs with typeorm

I am fairly new to Backend with NestJs and TypeORM, building a small reports application and I am having issues running migration as I have been using SQLite to get the project running.

npm run migration:generate -- src/migrations

When I run the command above to generate the migration, I get the following response

query: SELECT VERSION() AS `version`
query: SELECT * FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA` = 'reports_db' AND `TABLE_NAME` = 'typeorm_metadata'
No changes in database schema were found - cannot generate a migration. To create a new empty migration use "typeorm migration:create" command

npm run migration:run

When I also run the above command, I get the following response

query: SELECT VERSION() AS `version`
query: SELECT * FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA` = 'reports_db' AND `TABLE_NAME` = 'migrations'
query: SELECT * FROM `reports_db`.`migrations` `migrations` ORDER BY `id` DESC
No migrations are pending.

Below is my package.json

    "typeorm": "npm run build && npx typeorm -d dist/db/data-source.js",
    "migration:generate": "npm run typeorm -- migration:generate",
    "migration:run": "npm run typeorm -- migration:run",
    "migration:revert": "npm run typeorm -- migration:revert",
    "migration:create": "npm run typeorm -- migration:create",
    "migration:drop": "npm run typeorm -- schema:drop"

Below is my data-source.ts

import { DataSource, DataSourceOptions } from 'typeorm';
import * as dotenv from 'dotenv';

dotenv.config(); 

export const dataSourceOptions: DataSourceOptions = {
  type: 'mysql',
  host: process.env.DB_HOST,
  port: Number(process.env.DB_PORT),
  username: process.env.DB_USER,
  password: process.env.DB_PASSWORD,
  database: process.env.DB_NAME,
  logging: true,
  entities: [__dirname + '/entities/**/*.{js,ts}'],
  migrations: [__dirname + '/dist/migrations/*.js'],
  synchronize: false,
};

const dataSource = new DataSource(dataSourceOptions);

module.exports = dataSource;

Here is my user.entity

import { Report } from '../reports/report.entity';
import {
  Entity,
  Column,
  PrimaryGeneratedColumn,
  CreateDateColumn,
  UpdateDateColumn,
  OneToMany,
} from 'typeorm';
import { v4 as uuid } from 'uuid';

@Entity()
export class User {
  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column()
  firstName: string;

  @Column()
  lastName: string;

  @Column({ unique: true })
  email: string;

  @Column()
  password: string;

  @OneToMany(() => Report, (report) => report.user)
  reports: Report[];

  @CreateDateColumn({ type: 'timestamp' })
  createdAt: Date;

  @UpdateDateColumn({ type: 'timestamp' })
  updatedAt: Date;

  constructor() {
    this.id = uuid();
  }
}

The issue after all is that I dont see the schemas/tables for users and reports generated in mysql workbench.

I have also added a screenshot of my file structure for more information

Am I running the wrong commands? what can I do differently to get it to work?

Upvotes: 1

Views: 40

Answers (0)

Related Questions