Skip to content

Generate Migration TypeORM #113

Description

@juanzgc

Describe the bug
Generating a migration results in errors if the Entity that you are overriding has column types of other Entities (that are imported).

TypeORMError: Entity metadata for Product#images was not found. Check if you specified a correct entity object and if it's connected in the connection options.
    at new TypeORMError (/Users/juan/renlo/node_modules/src/error/TypeORMError.ts:7:9)
    at /Users/juan/renlo/node_modules/src/metadata-builder/EntityMetadataBuilder.ts:687:23
    at Array.forEach (<anonymous>)
    at EntityMetadataBuilder.computeInverseProperties (/Users/juan/renlo/node_modules/src/metadata-builder/EntityMetadataBuilder.ts:682:34)
    at /Users/juan/renlo/node_modules/src/metadata-builder/EntityMetadataBuilder.ts:119:56
    at Array.forEach (<anonymous>)
    at EntityMetadataBuilder.build (/Users/juan/renlo/node_modules/src/metadata-builder/EntityMetadataBuilder.ts:119:25)
    at ConnectionMetadataBuilder.<anonymous> (/Users/juan/renlo/node_modules/src/connection/ConnectionMetadataBuilder.ts:65:111)
    at step (/Users/juan/renlo/node_modules/tslib/tslib.js:144:27)
    at Object.next (/Users/juan/renlo/node_modules/tslib/tslib.js:125:57)
error Command failed with exit code 1.

For example:

  • Product: [images, options, variants, profile, collection, type, tags, sales_channel]
  • User: []

Whereas User doesn't face this error because there are no column types of other imported entities.

It seems that for whatever reason, when generating the migration with extended Entities, typeorm has difficulty with nested imports.

@medusajs/medusa/dist/models/product.d.ts:
image

@medusajs/medusa/dist/models/user.d.ts:
image

To Reproduce
Steps to reproduce the behavior:

  1. Create a ormconfig.json
{
  "type": "postgres",
  "url": "<INSERT>",
  "synchronize": false,
  "entities": ["dist/modules/**/*.entity.js"],
  "migrations": ["dist/modules/migrations/*.js"],
  "cli": {
    "migrationsDir": "src/modules/migrations"
  }
}
  1. Add the following scripts in package.json:
    "typeorm": "node --require ts-node/register ./node_modules/typeorm/cli.js",
    "migration:generate": "yarn typeorm migration:generate",
  1. Install typeorm:
yarn add typeorm@0.2.45
  1. Extend the Product Entity
import { Column, Entity, Index } from 'typeorm';
import { Product as MedusaProduct } from '@medusajs/medusa/dist/models';
import { Entity as MedusaEntity } from 'medusa-extender';

@MedusaEntity({ override: MedusaProduct })
@Entity()
export class Product extends MedusaProduct {
  @Index()
  @Column({ nullable: false })
  store_id: string;
}
  1. Generate Migration:
yarn build
yarn typeorm migration:generate -n InitialProductMigration
  1. You will see the error Entity metadata for Product#images was not found. However, if you add an export to the Product Entity extender you won't see the same error. The error will now be Entity metadata for Product#options was not found.
import { Column, Entity, Index } from 'typeorm';
import { Product as MedusaProduct } from '@medusajs/medusa/dist/models';
import { Entity as MedusaEntity } from 'medusa-extender';

export { Image } from '@medusajs/medusa/dist/models';

@MedusaEntity({ override: MedusaProduct })
@Entity()
export class Product extends MedusaProduct {
  @Index()
  @Column({ nullable: false })
  store_id: string;
}

The line changed was: export { Image } from '@medusajs/medusa/dist/models';

Don't forget to yarn build before re-attempting to generate the migration.

TypeORMError: Entity metadata for Product#options was not found. Check if you specified a correct entity object and if it's connected in the connection options.

You can continue adding all the remaining exports until the migration is generated, however, it will also generate a faulty migration because it will include multiple FKs and Indexes that it attempts to delete. I believe if we could somehow move all Entities into one file and export from one file (at build time), we would no longer see these migration errors.

You can also try this with an easier Entity such as Store which has less Columns that are referenced by other Entities

Expected behavior
Generate a migration without having to go through these hacks to get one generated. Generating migrations in the Medusa Core doesn't have these issues.

Screenshots
If applicable, add screenshots to help explain your problem.

Package version:

  • Version 1.7.4

Additional context
Similar Issue

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions