how can i create a new migration in nest? i have this file data-source.ts
import { DataSource } from 'typeorm';
import { User } from './models/user.entity';
export const AppDataSource = new DataSource({
type: 'postgres',
host: 'localhost',
port: 5432,
username: 'postgres',
password: 'password',
database: 'postgres',
entities: [User],
synchronize: false,
logging: true,
migrations: ['src/db/migrations/*.ts'],
});
this command i am using, i want to generate a migration for User
npx typeorm migration:generate User -d src/data-source.ts
but i am getting this error
Error during migration generation:
Error: Unable to open file: "E:\...\src\data-source.ts". Cannot find module 'E:\...\src\models\user.entity' imported from E:\...\src\data-source.ts
at CommandUtils.loadDataSource (E:\...\node_modules\typeorm\commands\CommandUtils.js:20:19)
at async Object.handler (E:\...\node_modules\typeorm\commands\MigrationGenerateCommand.js:73:26)
the User class is here
import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';
@Entity()
export class User {
@PrimaryGeneratedColumn()
id: number;
@Column({ type: 'varchar', length: 255, unique: true })
username: string;
@Column({ type: 'varchar', length: 255, unique: true })
email: string;
@Column({ type: 'varchar', length: 255 })
password: string;
@Column({ type: 'varchar', length: 255, nullable: true })
fullName: string;
}
the structure is:
how can i create a new migration in nest? i have this file data-source.ts
import { DataSource } from 'typeorm';
import { User } from './models/user.entity';
export const AppDataSource = new DataSource({
type: 'postgres',
host: 'localhost',
port: 5432,
username: 'postgres',
password: 'password',
database: 'postgres',
entities: [User],
synchronize: false,
logging: true,
migrations: ['src/db/migrations/*.ts'],
});
this command i am using, i want to generate a migration for User
npx typeorm migration:generate User -d src/data-source.ts
but i am getting this error
Error during migration generation:
Error: Unable to open file: "E:\...\src\data-source.ts". Cannot find module 'E:\...\src\models\user.entity' imported from E:\...\src\data-source.ts
at CommandUtils.loadDataSource (E:\...\node_modules\typeorm\commands\CommandUtils.js:20:19)
at async Object.handler (E:\...\node_modules\typeorm\commands\MigrationGenerateCommand.js:73:26)
the User class is here
import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';
@Entity()
export class User {
@PrimaryGeneratedColumn()
id: number;
@Column({ type: 'varchar', length: 255, unique: true })
username: string;
@Column({ type: 'varchar', length: 255, unique: true })
email: string;
@Column({ type: 'varchar', length: 255 })
password: string;
@Column({ type: 'varchar', length: 255, nullable: true })
fullName: string;
}
the structure is:
Share Improve this question edited Mar 24 at 17:00 information asked Mar 23 at 17:25 informationinformation 1156 bronze badges 2- @DarkBee do you have a asnwer perhaps? – information Commented Mar 24 at 16:21
- if i use -n like this: npx typeorm migration:generate -n create-user -d src/data-source.ts i got an error: Not enough non-option arguments: got 0, need at least 1 – information Commented Mar 24 at 17:00
1 Answer
Reset to default -1TypeORM can't locate your User entity file based on the import path in your data-source.ts
file. you need to properly import the entity. Also, The typeorm
CLI expects migration names as strings, so specify it properly by running npx typeorm migration:generate -n UserMigration -d src/data-source.ts