question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't run migrations if using other modules in entities

See original GitHub issue

Describe the bug I have an entity with some methods that uses an external module, because of that I can’t run the migrations (I have to comment the code where I use the module to be able to, which is ok on local/dev machine, but on the prod machine where it is configured to auto-deploy from the git master branch, I can’t do that).

Stack trace

{ Error: Cannot find module 'alt-server'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
    at Function.Module._load (internal/modules/cjs/loader.js:562:25)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Object.<anonymous> (/home/altv/actions-runner/_work/ggez_altv/ggez_altv/server/src/database/PlayerDataManager.ts:1:1)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Module.m._compile (/home/altv/actions-runner/_work/ggez_altv/ggez_altv/server/node_modules/ts-node/src/index.ts:814:23)
    at Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Object.require.extensions.(anonymous function) [as .ts] (/home/altv/actions-runner/_work/ggez_altv/ggez_altv/server/node_modules/ts-node/src/index.ts:817:12)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Object.<anonymous> (/home/altv/actions-runner/_work/ggez_altv/ggez_altv/server/src/database/index.ts:2:1)
    at Module._compile (internal/modules/cjs/loader.js:778:30) code: 'MODULE_NOT_FOUND' }

To Reproduce Steps to reproduce the behavior:

  1. Add some methods that are not annotated with @Property or anything DB related
  2. Use an external module inside that method
  3. Try to run npx mikro-orm migration:create or npx mikro-orm migration:up

Expected behavior That module should be ignored as there isn’t anything related to a db(@Property) accessing the module and migrations should be run

Additional context This is the mikro-orm config I’m using:

import {Options, ReflectMetadataProvider} from "mikro-orm";
import Account from "./_entities/Account";
import PlayerLevel from "./_entities/PlayerLevel";
import PlayerMoney from "./_entities/PlayerMoney";
import {MYSQL_DATABASE, MYSQL_HOST, MYSQL_PASSWORD, MYSQL_USERNAME} from "./utils/config";

const config = {
    metadataProvider: ReflectMetadataProvider,
    discovery: {disableDynamicFileAccess: true},
    cache: {enabled: false},
    entities: [Account, PlayerLevel, PlayerMoney],
    dbName: MYSQL_DATABASE,
    host: MYSQL_HOST,
    port: 3306,
    user: MYSQL_USERNAME,
    password: MYSQL_PASSWORD,
    type: "mysql"
} as Options;

export default config;

And this is my entity:

import Account from "./Account";
import {DatabaseEvents, DatabaseEventType} from "../database";
import {Entity, IdEntity, OneToOne, PrimaryKey, Property} from "mikro-orm";

@Entity()
export default class PlayerLevel implements IdEntity<PlayerLevel> {

    @PrimaryKey()
    id: number;

    @OneToOne(() => Account)
    user: Account;

    @Property({default: 1, name: 'level', type: 'number'})
    private _level: number = 1;

    get level(): number {
        return this._level;
    }

    set level(value: number) {
        if (this.user.playerObj)
            DatabaseEvents.getInstance().emit(DatabaseEventType.PlayerLevel.Level, this.user.playerObj, this._level, value);
        this._level = value;
    }


    @Property({default: 0, name: 'role_points', type: 'number'})
    private _rolePoints: number = 0;

    get rolePoints(): number {
        return this._rolePoints;
    }

    set rolePoints(value: number) {
        if (this.user.playerObj)
            DatabaseEvents.getInstance().emit(DatabaseEventType.PlayerLevel.RolePoints, this.user.playerObj, this._rolePoints, value);
        this._rolePoints = value;
    }

    @Property({name: 'created_at', onCreate: () => new Date(), type: 'date'})
    createdAt: Date;

    @Property({name: 'updated_at', onUpdate: () => new Date(), type: 'date'})
    updatedAt: Date;
}

Versions

Dependency Version
node 10.16.3
typescript 3.7.5
mikro-orm 3.3.5
mysql2 2.1.0

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:21 (21 by maintainers)

github_iconTop GitHub Comments

1reaction
robertnisipeanucommented, Mar 6, 2020

Solved in #384 so I will close this issue.

0reactions
B4nancommented, Mar 5, 2020

I would go for emit: 'js' | 'ts';, ideally we should base the default value on ts-node support being detected or forced via tsNode flag. But for simplicity let’s start with 'ts' as the default.

That build error might be some dependency mismatch (maybe TS version? but that should be locked), try to use yarn so you install based on the lock file.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't run makemigrations after moving modules - Stack Overflow
I've got a module named: validators.py located at ./project/app1/validators.py . To use it, I have imported it inside one of my Models:
Read more >
Using a Separate Migrations Project - EF Core | Microsoft Learn
If you have no existing migrations, generate one in the project containing the DbContext then move it.
Read more >
Migrations often do not exist or can't be found - Drupal
Usually I'm trying to run a migration, so I start with mi and then run ms when that is not found, which results...
Read more >
EF Core Database Migrations - ABP Documentation
You want to add a new field to the table and map it to a property in the entity. You can't use the...
Read more >
Executing Drupal migrations from the user interface with ...
Note: If you do not see the “Migration” and “Migration group” configuration types, you need to enable the Migrate Plus module. Another thing...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found