typeorm: synchronize=true in connection does not update database schema in DB

Issue Description

Setting synchronize: true in createConnection does not update schema in DB (postgres 9.6) if there has been any change in entity schema after table is created. This issue occured in 0.2.34, 0.2.35. Issue does not exist in 0.2.33.

Expected Behavior

With synchronize: true, any change in entity should be updated in related database table.

Actual Behavior

With synchronize: true, table is created if it does not exist, but any further change in entity schema do not lead to change in database table.

Steps to Reproduce

  1. Set synchronize=true in createConnection option
  2. Update entity schema (e.g. adding a index) for a entity for which table already exits in database
  3. The entity schema update is not reflected in database table (e.g Newly added index is not created in database).
// Options used for creating connection
const conn = await createConnection({
                type: 'postgres',
                host: process.env.DB_HOST as string,
                port: parseInt(process.env.DB_PORT as string, 10),
                username: process.env.DB_USERNAME as string,
                password: process.env.DB_PASSWORD as string,
                database: process.env.DB_NAME as string,
                schema: 'public',
                logging: true,
                entities,
                namingStrategy: new SnakeNamingStrategy(),
                synchronize: true,
            });

My Environment

Dependency Version
Operating System MacOS Intel Big Sur 11.5
Node.js version 14.17.3
Typescript version 4.3.5
TypeORM version 0.2.35

Additional Context

Issue does not exist in version 0.2.33. Noticed in 0.2.34 and 0.2.35.

Relevant Database Driver(s)

DB Type Reproducible
aurora-data-api no
aurora-data-api-pg no
better-sqlite3 no
cockroachdb no
cordova no
expo no
mongodb no
mysql no
nativescript no
oracle no
postgres yes
react-native no
sap no
sqlite no
sqlite-abstract no
sqljs no
sqlserver no

Are you willing to resolve this issue by submitting a Pull Request?

  • ✖️ Yes, I have the time, and I know how to start.
  • ✅ Yes, I have the time, but I don’t know how to start. I would need guidance.
  • ✖️ No, I don’t have the time, but I can support (using donations) development.
  • ✖️ No, I don’t have the time and I’m okay to wait for the community / maintainers to resolve this issue.

About this issue

  • Original URL
  • State: open
  • Created 3 years ago
  • Comments: 15 (7 by maintainers)

Most upvoted comments

Yep this isn’t working. Would be nice to get this fixed 😃

In my case, the culprit is running the createDatabase from typeorm-extension , await createDatabase({ ifNotExist: true, options: dataSourceOptions }), before the initialisation of the dataSource AppDataSource.initialize(). Uncommenting the createDatabase fixes it.

Confirmed. Temporary solution is to remove schema: 'public', - by default it is already public and you can omit it.