skip to Main Content

I’ve read about 100 stack overflow posts, a few dozen reddit posts, and watched a handful of youtube videos. I cannot for the life of me get typeorm to generate a migration.

I’m quite sure the problem is that the entities glob is not catching my entities but nothing I try works.

here’s my data source file

import { DataSource, DataSourceOptions } from "typeorm";

export const dataSourceOptions: DataSourceOptions = {
  type: 'postgres',
  host: 'localhost',
  port: 5432,
  username: 'postgres',
  password: 'example',
  database: 'postgres',
  migrations: ['.src/migrations/*{.ts,.js}'],
  // entities: ['src/entities/*{.ts,.js}'],
  // entities: ['./src/entities/*{.ts,.js}'],
  // entities: ['dist/src/entities/*{.ts,.js}'],
  // entities: ['./dist/src/entities/*{.ts,.js}'],
  // entities: [`${__dirname}/src/entities/*{.ts,.js}`],
  // entities: [`${__dirname}/dist/src/entities/*{.ts,.js}`],
  // entities: [Item,Brand,Product,Size,Value], // this one came with import statements too
  entities: ['dist/src/entities/*{.ts,.js}'],
}

const dataSource = new DataSource(dataSourceOptions);

export default dataSource;

the comments are all of the different things I’ve tried to get it to work

my code is a NestJs project that is structured like

project-name
  ├──data-source.ts
  ├──src
  │  └──entities
  │      └──item.js
  │      └──brand.js
  │      └──size.js
  │      └──value.js
  │      └──product.js

heres an example entity

import { Entity, Column, PrimaryGeneratedColumn, ManyToOne, JoinColumn } from 'typeorm';
import { Brand } from './brand';

@Entity()
export class Product {
  @PrimaryGeneratedColumn("uuid")
  id: string;

  @Column()
  msrp: number;

  @Column()
  name: string;

  @ManyToOne(type => Brand)
  @JoinColumn({ name: "brand" })
  brand: Brand;
}

the application works if I synchronize via nest but that’s not the point so i emptied the database.

> docker exec -it 0dcfd4457db2 sh
# psql -U postgres
psql (15.2 (Debian 15.2-1.pgdg110+1))
Type "help" for help.

postgres=# dt
Did not find any relations.
postgres=# d
Did not find any relations.

my dependencies:

  "dependencies": {
    "@nestjs/common": "^9.0.0",
    "@nestjs/config": "^2.3.1",
    "@nestjs/core": "^9.0.0",
    "@nestjs/mapped-types": "*",
    "@nestjs/platform-express": "^9.0.0",
    "@nestjs/typeorm": "^9.0.1",
    "pg": "^8.10.0",
    "reflect-metadata": "^0.1.13",
    "rxjs": "^7.2.0",
    "typeorm": "^0.3.12"
  },
  "devDependencies": {
    "@nestjs/cli": "^9.0.0",
    "@nestjs/schematics": "^9.0.0",
    "@nestjs/testing": "^9.0.0",
    "@types/express": "^4.17.13",
    "@types/jest": "29.2.4",
    "@types/node": "18.11.18",
    "@types/supertest": "^2.0.11",
    "@typescript-eslint/eslint-plugin": "^5.0.0",
    "@typescript-eslint/parser": "^5.0.0",
    "eslint": "^8.0.1",
    "eslint-config-prettier": "^8.3.0",
    "eslint-plugin-prettier": "^4.0.0",
    "jest": "29.3.1",
    "prettier": "^2.3.2",
    "source-map-support": "^0.5.20",
    "supertest": "^6.1.3",
    "ts-jest": "29.0.3",
    "ts-loader": "^9.2.3",
    "ts-node": "^10.9.1",
    "tsconfig-paths": "4.1.1",
    "typescript": "^4.7.4"
  },

for each of the above data source configs i’ve tried: npx’ing typeorm, typeorm-ts-node-esm, typeorm-ts-node-commonjs. i’ve tried various package.json scripts such as:"typeorm": "npm run build && npx typeorm -d ./data-source.js", "typeorm": "./node_modules/.bin/ts-node ./node_modules/.bin/typeorm", and "typeorm": "node --require ts-node/register ./node_modules/typeorm/cli.js"

in every single case where I don’t get an error I get No changes in database schema were found - cannot generate a migration.

2

Answers


  1. As you mentioned, unfortunately, TypeORM cannot detect entities using directories. I can’t find any solutions either.

    To resolve this issue, you have to manually import all your entities and pass them to the entities option.

     import { Product } from 'path/to/product/entity';
     import { DataSource, DataSourceOptions } from "typeorm";
    
    export const dataSourceOptions: DataSourceOptions = {
      type: 'postgres',
      host: 'localhost',
      port: 5432,
      username: 'postgres',
      password: 'example',
      database: 'postgres',
      migrations: ['.src/migrations/*{.ts,.js}'],
      // entities: ['src/entities/*{.ts,.js}'],
      // entities: ['./src/entities/*{.ts,.js}'],
      // entities: ['dist/src/entities/*{.ts,.js}'],
      // entities: ['./dist/src/entities/*{.ts,.js}'],
      // entities: [`${__dirname}/src/entities/*{.ts,.js}`],
      // entities: [`${__dirname}/dist/src/entities/*{.ts,.js}`],
      // entities: [Item,Brand,Product,Size,Value], // this one came with import statements too
      entities: [Product],
    }
    
    const dataSource = new DataSource(dataSourceOptions);
    
    export default dataSource;
    
    Login or Signup to reply.
  2. You Can follow this:
    https://github.com/MIShuvro/dream-tour

    My Typeorm Version:
    "@nestjs/typeorm": "^9.0.1",
    "typeorm": "^0.3.7"
    

    ** My Orm Config File:**

    const dataSource = new DataSource({
      type: process.env.DB_DRIVER as any,
      host: process.env.DB_HOST,
      port: +process.env.DB_PORT,
      username: process.env.DB_USER,
      password: process.env.DB_PASSWORD,
      database: process.env.DB_NAME,
      entities: ['dist/**/*.entity{.ts,.js}'],
      synchronize: false,
      migrationsRun: process.env.RUN_MIGRATION === 'true',
      logging: process.env.DB_LOG_ENABLED === 'true',
      migrations: ['dist/migrations/*.js'],
    });
    

    package.json

     "migration:generate": "ENV_PATH='.env' typeorm -d ./dist/ormconfig.js migration:generate ./migrations/$npm_config_name",
        "db:migrate": "ENV_PATH='.env' typeorm migration:run -d 
     ./dist/ormconfig.js",
        "db:revert": "ENV_PATH='.env' typeorm -d ./dist/ormconfig.js 
        migration:revert",
        "db:sync": "ENV_PATH='.env' typeorm -d ./dist/ormconfig.js schema:sync",
        "db:log-sync": "ENV_PATH='.env' typeorm -d ./ormconfig.js schema:log"
        ```
    
    
      [1]: https://github.com/MIShuvro/dream-tour
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search