Arbaz Irshad
Arbaz Irshad

Reputation: 809

Mikro-Orm not creating a sqlite table

I am using mikro-orm in nestjs to create a sqlite database. For some reason, Mikro-Orm will create the database file(sandox.sqlite3) but will not create tables in the database. I have created a task module with entities.

App Module:

@Module({
  imports: [
    MikroOrmModule.forRoot({
      entities: ['./dist/**/entities/*'],
      entitiesTs: ['./src/**/entities/*'],
      dbName: 'sandbox.sqlite3',
      type: 'sqlite',
    }),
    TasksModule,
  ],
  controllers: [AppController],
  providers: [AppService],
})
export class AppModule {}

TaskModule:

@Module({
  imports: [MikroOrmModule.forFeature([Task])],
  controllers: [TasksController],
  providers: [TasksService],
})
export class TasksModule {}

Task Service: When i make a post request to create, A 201 response is received back along with the data that i sent. But there is not table created in the database.

@Injectable()
export class TasksService {
  constructor(
    @InjectRepository(Task)
    private readonly taskRepository: EntityRepository<Task>,
  ) {}

  create(createTaskDto: CreateTaskDto) {
    console.log(createTaskDto.name);
    return this.taskRepository.create({ ...createTaskDto });
  }

  findAll() {
    return this.taskRepository.findAll();
  }

  findOne(id: number) {
    return this.taskRepository.findOne({ id });
  }

  async update(id: number, updateTaskDto: UpdateTaskDto) {
    const task = await this.taskRepository.findOne({ id });
    task.name = updateTaskDto.name ?? task.name;
    task.description = updateTaskDto.description ?? task.description;
    task.assign({ id });
    // return this.taskRepository.(task);
  }

  remove(id: number) {
    // return this.taskRepository.removeAndFlush();
  }
}

Task Entity:

@Entity()
export class Task extends BaseEntity<Task, 'id'> {
  @PrimaryKey()
  id: number;

  @Property()
  name: string;

  @Property()
  description: string;
}

Upvotes: 2

Views: 2439

Answers (1)

Martin Ad&#225;mek
Martin Ad&#225;mek

Reputation: 18389

You need to sync the database manually, either via SchemaGenerator or via migrations. If you have CLI set up, you can do npx mikro-orm schema:update --run

https://mikro-orm.io/docs/faq#how-can-i-synchronize-my-database-schema-with-the-entities

Upvotes: 4

Related Questions