Postgres array column dropped and re-added during synchronization when no change is made.
See original GitHub issueIssue 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
- Configure Typeorm with a Postgres connection.
- Enable synchronization.
- Configure an entity with a column of type string[] and options set to { array: true, type: “character varying”, length: 64 }.
- 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:
- Created 3 years ago
- Comments:8 (4 by maintainers)
Top GitHub Comments
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.
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.