FG
๐Ÿ—„๏ธ Databases

"synchronize" option removing data from the database

Freshabout 21 hours ago
Mar 14, 20260 views
Confidence Score60%
60%

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

Canonical Fix
Unverified Fix
New Fix โ€“ Awaiting Verification

Disable Synchronize Option to Prevent Data Loss

Medium Risk

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. 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.

    typescript
    const connectionOptions = { synchronize: false, /* other options */ };
  2. 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.

    sql
    ALTER TABLE Accounts MODIFY company VARCHAR(255); ALTER TABLE Accounts MODIFY active TINYINT(1);
  3. 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.

    bash
    typeorm migration:generate -n UpdateAccountsSchema
  4. 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.

    bash
    npm run test
  5. 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.

    bash
    mysqldump -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

AC

Alex Chen

2450 rep

Tags

typeormormtypescriptquestiondesign-issue:-directory-loaded-entities