FG
๐Ÿ’ป Software๐Ÿ—„๏ธ Databases

TypeORM Attempts to create tables that already exist

Fresh3 days ago
Mar 14, 20260 views
Confidence Score62%
62%

Problem

Issue type: [ ] question [x ] bug report [ ] feature request [ ] documentation issue Database system/driver: [ ] `cordova` [ ] `mongodb` [x ] `mssql` [ ] `mysql` / `mariadb` [ ] `oracle` [ ] `postgres` [ ] `sqlite` [ ] `sqljs` [ ] `react-native` [ ] `expo` TypeORM version: [ x] `latest` [ ] `@next` [ ] `0.x.x` (or put your version here) Steps to reproduce or a small repository showing the problem: If I delete all the tables in my database and run my app, all tables are created successfully. If I then stop the app and restart it, typeorm attempts to create tables that already exist and then fails. Here is the output from TypeOrm for the second run: Here is the beginning of the definition for the EmployeeAvailabilities table (there are more columns than this): [code block] At first I thought the issue was because the class name and table name do not match. But if I make the class name and table name match, the issue still occurs.

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix โ€“ Awaiting Verification

Prevent TypeORM from Attempting to Create Existing Tables

Medium Risk

TypeORM may attempt to synchronize the database schema on every application start, which can lead to errors if the tables already exist. This behavior is influenced by the `synchronize` option in the TypeORM configuration, which defaults to true. If the application tries to create tables that are already present in the database, it will throw an error.

Awaiting Verification

Be the first to verify this fix

  1. 1

    Check TypeORM Configuration

    Ensure that the TypeORM configuration does not have the `synchronize` option set to true if you are managing the database schema manually.

    typescript
    const connectionOptions = { type: 'mssql', host: 'localhost', username: 'user', password: 'password', database: 'db', synchronize: false };
  2. 2

    Use Migrations for Schema Changes

    Instead of relying on automatic schema synchronization, create and run migrations to manage database schema changes. This will prevent TypeORM from trying to create existing tables.

    bash
    typeorm migration:generate -n MigrationName
    typeorm migration:run
  3. 3

    Verify Existing Tables

    Before running the application, manually check the database to ensure that the required tables exist. If they do not, you can create them using the generated migration or manually.

    sql
    SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'EmployeeAvailabilities';
  4. 4

    Handle Errors Gracefully

    Implement error handling in your application to catch and log errors related to table creation. This will help diagnose issues if they arise again in the future.

    typescript
    try { await connection.synchronize(); } catch (error) { console.error('Error during schema synchronization:', error); }

Validation

After applying these changes, restart the application and confirm that no errors related to existing tables are thrown. Check the application logs to ensure that the database schema is managed correctly without attempts to recreate existing tables.

Sign in to verify this fix

Environment

Submitted by

AC

Alex Chen

2450 rep

Tags

typeormormtypescriptcomp:-schema-syncdriver:-mssql