Fix MikroORM Entity Generate β Entities Not Generated From Database
DodaTech
Updated 2026-06-24
3 min read
In this tutorial, you'll learn about Fix MikroORM Entity Generate. We cover key concepts, practical examples, and best practices.
You run npx mikro-orm schema:create -r to generate entities from an existing database. The command runs, but no entity files appear β or the generated entities are missing relations, indexes, or custom types.
Wrong β
npx mikro-orm schema:create -r
# or
npx mikro-orm import -n
Output:
No entities were generated.
Your database has 15 tables, but MikroORM didn't create any entity files. The database connection works (no errors), but generation produces nothing.
## Right β
```<a href="/programming-languages/typescript/">typescript</a>
// mikro-orm.config.ts
import { defineConfig } from '@mikro-orm/<a href="/databases/postgresql/">postgresql</a>';
export default defineConfig({
entities: ['./dist/entities'], // output directory for generated entities
entitiesTs: ['./src/entities'], // <a href="/programming-languages/typescript/">TypeScript</a> source directory
dbName: 'mydb',
host: 'localhost',
port: 5432,
user: 'postgres',
password: 'password',
discovery: {
warnWhenNoEntities: false,
},
// For generating from database:
schemaGenerator: {
disableForeignKeys: false,
createForeignKeyConstraints: true,
},
});
```bash
# Generate entities from the database schema
npx mikro-orm schema:create -r --dump
# Dumps SQL that would be executed β not what we want
# Instead, use the entity generator:
npx mikro-orm entity:generate --path ./src/entities --save
**Or use the `discoverEntities` script:**
```bash
npx mikro-orm discover
Expected output:
Entities generated:
- User (created)
- Post (created)
- Comment (created)
- Category (created)
Done in 1.2s
**If specific tables are missing:**
Check that MikroORM can access them:
```bash
npx mikro-orm schema:generate --dump
# Shows all tables detected
**Generated entity file:**
```<a href="/programming-languages/typescript/">typescript</a>
// src/entities/User.ts
import { Entity, PrimaryKey, Property, OneToMany, Collection } from '@mikro-orm/core';
@Entity()
export class User {
@PrimaryKey()
id!: number;
@Property()
name!: string;
@Property({ unique: true })
email!: string;
@Property({ type: 'timestamp', defaultRaw: 'now()' })
createdAt!: Date;
@OneToMany(() => Post, (post) => post.author)
posts = new Collection<Post>(this);
}
**Run the entity generator with verbose output:**
```bash
npx mikro-orm entity:generate --path ./src/entities --save --verbose
## Root Cause
MikroORM's `schema:create -r` generates SQL from entities (the reverse direction). To generate entities from an existing database, you need `entity:generate`. The `discover` command can also create entity files. Misunderstanding these CLI commands leads to "no entities generated" confusion.
## Prevention
- Use `entity:generate --save` to create entity files from an existing database.
- Use `schema:create -r` to generate SQL from entities (e.g., for migration).
- Specify `--path` to control where entity files are written.
- Run with `--verbose` to see what the generator is doing.
## Common Mistakes with entity generate
1. **Mixing let bindings with <- bindings in do notation, producing type errors**
2. **Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors**
3. **Non-exhaustive pattern matches that compile with warnings then crash at runtime**
These mistakes appear frequently in real-world MIKROORM code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
## Practice Exercise
**Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.**
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
## FAQ
<details style="margin-bottom:12px;border:1px solid #e2e8f0;border-radius:10px;overflow:hidden"><summary style="cursor:pointer;padding:14px 18px;font-weight:600;font-size:1.05rem;background:#f8fafc;border-bottom:1px solid #e2e8f0;color:#1e293b">**Q: Can I regenerate entities after schema changes?**</summary><div style="padding:14px 18px;color:#475569;line-height:1.7;background:#fff"><p>A: Yes β reβrun <code>entity:generate</code>. It overwrites existing files, so back up custom modifications.</p>
</div></details><details style="margin-bottom:12px;border:1px solid #e2e8f0;border-radius:10px;overflow:hidden"><summary style="cursor:pointer;padding:14px 18px;font-weight:600;font-size:1.05rem;background:#f8fafc;border-bottom:1px solid #e2e8f0;color:#1e293b">**Q: Does MikroORM support generating entities for all database providers?**</summary><div style="padding:14px 18px;color:#475569;line-height:1.7;background:#fff"><p>A: Yes β <a href="/databases/postgresql/">PostgreSQL</a>, MySQL, SQLite, <a href="/databases/mariadb/">MariaDB</a>, and <a href="/databases/mongodb/">MongoDB</a> (with limitations).</p>
</div></details><details style="margin-bottom:12px;border:1px solid #e2e8f0;border-radius:10px;overflow:hidden"><summary style="cursor:pointer;padding:14px 18px;font-weight:600;font-size:1.05rem;background:#f8fafc;border-bottom:1px solid #e2e8f0;color:#1e293b">**Q: How do I customise the generated entity?**</summary><div style="padding:14px 18px;color:#475569;line-height:1.7;background:#fff"><p>A: Edit the <code>.ts</code> files after generation. MikroORM doesn't reβapply manual changes.</p>
</div></details><details style="margin-bottom:12px;border:1px solid #e2e8f0;border-radius:10px;overflow:hidden"><summary style="cursor:pointer;padding:14px 18px;font-weight:600;font-size:1.05rem;background:#f8fafc;border-bottom:1px solid #e2e8f0;color:#1e293b">**Q: What's the difference between `entity:generate` and `schema:create`?**</summary><div style="padding:14px 18px;color:#475569;line-height:1.7;background:#fff"><p>A: <code>entity:generate</code> creates entity files from the database. <code>schema:create</code> creates SQL from entity files.</p>
</div></details>
---
*Entity generation is covered in the [DodaTech MikroORM Fundamentals course](https://dodatech.com/courses/mikroorm).*
β Previous
Fix Micronaut Validation Not Triggering
Next β
Fix MikroORM Migration Up β Migration Not Applied
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro