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
- Set
synchronize=truein createConnection option - Update entity schema (e.g. adding a index) for a entity for which table already exits in database
- 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 5 years ago
- Comments: 17 (7 by maintainers)
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 dataSourceAppDataSource.initialize(). Uncommenting the createDatabase fixes it.Confirmed. Temporary solution is to remove
schema: 'public',- by default it is alreadypublicand you can omit it.I know this may sound crazy but sometimes it’s a tiny issue that causes some ridiculous errors. I landed here because I had the same problem with database synchronization. I had just switched to another editor and added a new entity.
Please make sure you have autosave enabled on your editor! I laughed at myself when I found out that I had it disabled (Default behavior). Autosave off causes the files not to be saved thus resulting in a compilation excluding the newly added entity. After enabling the autosave, adjusting my code, and restarting the server, it worked like a charm!
Imagine that I did everything else before this, I deleted my database completely, deleted the dist folder, and restarted the server several times.
Hope you guys have a great time coding!