"synchronize" option removing data from the database
Problem
Issue type: [ ] question [] bug report [ ] feature request [ ] documentation issue Database system/driver: [ ] `cordova` [ ] `mongodb` [ ] `mssql` [] `mysql` / `mariadb` [ ] `oracle` [ ] `postgres` [ ] `sqlite` [ ] `sqljs` [ ] `react-native` [ ] `expo` TypeORM version: [*] `latest` [ ] `@next` [ ] `0.x.x` (or put your version here) Steps to reproduce or a small repository showing the problem: when i enable "synchronize" option, it constantly changing my database data in very specific table and specific data, its changing the "Accounts" entity, changing the "company" field to empty string and the "active" column to 0 where the id is 1 so my database looks like this: id | company | active 1 | blabla | 1 transformed into this on every change: id | company | active 1 | | 0 This is my account entity: [code block] and this is the ormconfig: [code block]
Unverified for your environment
Select your OS to check compatibility.
1 Fix
Disable Synchronize Option to Prevent Data Loss
The 'synchronize' option in TypeORM automatically syncs the database schema with the entity definitions. If there are discrepancies between the entity definitions and the existing database schema, it may lead to unintended data modifications, such as setting fields to default values (e.g., empty strings or zeros). In this case, the 'company' field is being set to an empty string and 'active' to 0 due to the mismatch in the entity definition and the existing data.
Awaiting Verification
Be the first to verify this fix
- 1
Disable Synchronize Option
Modify the TypeORM configuration to disable the 'synchronize' option. This will prevent TypeORM from automatically altering the database schema and data.
typescriptconst connectionOptions = { synchronize: false, /* other options */ }; - 2
Manually Update Database Schema
Review the current database schema and manually update it to match the entity definitions. This can be done using migration scripts or direct SQL commands.
sqlALTER TABLE Accounts MODIFY company VARCHAR(255); ALTER TABLE Accounts MODIFY active TINYINT(1); - 3
Create Migration for Future Changes
To manage future schema changes, create migration files using TypeORM's CLI. This ensures that any changes to the entities are reflected in the database without data loss.
bashtypeorm migration:generate -n UpdateAccountsSchema - 4
Test Changes in a Development Environment
Before deploying changes to production, test the updated configuration and migration scripts in a development environment to ensure that no data is lost and the application behaves as expected.
bashnpm run test - 5
Backup Database Regularly
Implement a regular backup strategy for the database to prevent data loss in case of future issues. This can be done using automated scripts or database management tools.
bashmysqldump -u username -p database_name > backup.sql
Validation
Confirm that the 'synchronize' option is disabled and that the database schema matches the entity definitions without any unintended data modifications. Run tests to ensure that data integrity is maintained.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep