question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Postgres array column dropped and re-added during synchronization when no change is made.

See original GitHub issue

Issue Description

When using Postgres, if a length is specified for an array of type varchar and synchronization is enabled, Typeorm will drop and re-add the column on startup even if there are no schema changes.

If the length property is removed, the behavior will stop occurring.

Expected Behavior

When there are no changes to the column, Typeorm should recognize the current database schema matches the column configuration, and not drop and re-add the column during synchronization.

Actual Behavior

When synchronizing on startup, Typeorm issues SQL queries to drop and re-add the column, even though nothing has changed.

From the console output at startup:

query: ALTER TABLE "test"."user" DROP COLUMN "roles"
query: ALTER TABLE "test"."user" ADD "roles" character varying(64) array NOT NULL

Steps to Reproduce

  1. Configure Typeorm with a Postgres connection.
  2. Enable synchronization.
  3. Configure an entity with a column of type string[] and options set to { array: true, type: “character varying”, length: 64 }.
  4. Launch the program to create the new column, and then restart it to trigger synchronization again.
 @Column({
    array: true,
    type: "character varying",
    length: 64,
  })
  roles: string[];

For a small example, see: https://github.com/edcolvin/array-sync-issue

My Environment

Dependency Version
Operating System CentOS 7
Node.js version v12.16.3
Typescript version 4.0.5
TypeORM version 0.2.28

Additional Context

Tested using Postgres:11 Docker image PostgreSQL 11.9 (Debian 11.9-1.pgdg90+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 6.3.0-18+deb9u1) 6.3.0 20170516, 64-bit

Relevant Database Driver(s)

  • aurora-data-api
  • aurora-data-api-pg
  • better-sqlite3
  • cockroachdb
  • cordova
  • expo
  • mongodb
  • mysql
  • nativescript
  • oracle
  • postgres
  • react-native
  • sap
  • sqlite
  • sqlite-abstract
  • sqljs
  • sqlserver

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, although I believe I could do it if I had the time…
  • No, I don’t have the time and I wouldn’t even know how to start.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
edcolvincommented, Dec 23, 2020

I just meant I don’t think it’s related to the MySQL issue you linked, as it seems to be a product of how Typeorm queries Postgres and how Postgres reports array columns.

I started work on a pull request for this issue. I’m not sure if my fix is the best way, but maybe I can get some feedback on it. I don’t see how it would break anything else, so at worst it is simply not very elegant. Once it’s ready to go I’ll link it here.

0reactions
kevinlulcommented, Dec 23, 2020

Hi Ed, I’m not sure what you mean. I am using Postgres and since we are in the early stages, I had synchronization enabled for rapid development. I was using Postgres arrays as they seemed to be the natural match for storing an array after all but I discovered the same pattern as you did with the array columns being dropped and recreated at startup. I found this issue after investigating and simply implemented Alexandru’s workaround with the json column type and we haven’t had problems with that yet.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation: 15: E.2. Release 15 - PostgreSQL
The change applies to new database clusters and to newly-created databases in existing clusters. Upgrading a cluster or restoring a database ...
Read more >
Documentation: 15: 8.15. Arrays - PostgreSQL
PostgreSQL allows columns of a table to be defined as variable-length multidimensional arrays. Arrays of any built-in or user-defined base type, enum type, ......
Read more >
Documentation: 14: E.7. Release 14 - PostgreSQL
User-defined objects that reference certain built-in array functions along ... or dropped columns when using attribute numbers (Joe Conway).
Read more >
Documentation: 12: E.14. Release 12 - PostgreSQL
This column has been deprecated for a long time, because it did not update in response to other catalog changes (such as column...
Read more >
8.0.0 - PostgreSQL: Release Notes
It is now possible to alter a column's data type without dropping and re-adding the column. Allow multiple ALTER actions in a single...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found