Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

TypeORM connects but creates no tables or columns

ErrorNo tables appear in the database after the app starts

Short answer

TypeORM does not create tables by itself. Either synchronize is on, or migrations run. Check that one of them is configured, that your entities are registered, and that initialize() was called and awaited before the first query. Do not leave synchronize on in production.

  • Applies to TypeORM 0.3.x, Node.js 22, PostgreSQL 16, MySQL 8
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print how the DataSource is set up: whether schema sync or auto-run migrations are on, and which entities it loaded.

    await AppDataSource.initialize()
    console.log(AppDataSource.options.synchronize, AppDataSource.options.migrationsRun)
    console.log(AppDataSource.entityMetadatas.length)
  2. 2Match what you see, then jump to its fix

    1. 1Both values print false or undefinedNeither synchronize nor migrations are doing the workFix
    2. 2entityMetadatas.length prints 0The entities are not registered, so there is nothing to createFix
    3. 3Queries fail or run before setup is doneinitialize() was never called, or not awaitedFix

What does the error look like?

  • No tables appear in the database after the app starts
  • A new column is missing, with no error at startup

Cause 1 of 3

Neither synchronize nor migrations are doing the work

1Check

The docs define synchronize as auto-creating the schema on every launch, and migrationsRun as running migrations on every launch. Both default to off.

grep -n -E "synchronize|migrationsRun" src/data-source.ts

2Fix

For local work set synchronize to true. For anything shared, turn it off and run migrations instead.

// local development only
synchronize: true,

// everywhere else
synchronize: false,
migrationsRun: true,

3Verify

Restart and list the tables.

psql "$DATABASE_URL" -c '\dt'

Confirmed by typeorm.io

Did this fix it?

Cause 2 of 3

The entities are not registered, so there is nothing to create

1Check

Synchronize builds the schema from the registered entities. With a glob that matches no files, it has nothing to build and says nothing.

grep -n "entities" src/data-source.ts

2Fix

Register the classes, or use a glob with both extensions based on __dirname.

entities: [__dirname + "/entities/**/*{.ts,.js}"],

3Verify

The first-step print shows a non-zero count and the tables appear.

psql "$DATABASE_URL" -c '\dt'

Confirmed by typeorm.io

Did this fix it?

Cause 3 of 3

initialize() was never called, or not awaited

1Check

The docs say you must call initialize once to open the connection, and that database access is only possible after setup.

grep -rn "initialize()" src

2Fix

Call initialize at startup and wait for it before the server starts listening.

await AppDataSource.initialize()
app.listen(3000)

3Verify

The app logs the connection before it accepts requests.

curl -i http://127.0.0.1:3000/health

Confirmed by typeorm.io

Did this fix it?

How do I stop it coming back?

  • Use synchronize only on a throwaway local database.
  • Generate a migration for every entity change and commit it.
  • Log the loaded entity count and the migration count at startup.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.