[Fix] not to make typeorm generate alter query on geometry column when that column was not changed #5525
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
Let's assume the entity like this.
Typeorm always think the geometry field has been changed even though I did not change it.
If synchronize option is true, typeorm always run below query whenever server started.
If synchronize option is false, when I execute
typeorm migration:generate
, it "always" make new migration file below even after I execute typeorm migration:run.When I run query below,
it returns
It means,
my_place
column's geometry type isPOINT
on postgresql which is correct, but as we can see in the result oftypeorm migration:generate
, typeorm think this column is notPOINT
butPOLYGON
.Therefore, typeorm keep trying to alter column's geometry type even though it set correctly.
This is not a critical bug, but typeorm execute useless alter query if synchronize option is true. It is pretty annoying when execute
typeorm migration:generate
if synchronize option is false.Cause
Let's see the code of
loadTables
method inPostgresQueryRunner.ts
.The
geometryColumnSql
execute the query like this.The result is
This is the problem.
Let's go back to the code.
results[0].type
is alwaysPOLYGON
. Therefore even though the columnmy_point
geometry type isPOINT
, typeorm always think it's type isPOLYGON
.Solution
First, we need to fix
WHERE
condition by wrapping OR condition in a parenthesis like this.Second, add table name on
WHERE
condition so that if we define same column name with different geometry type, the queryRunner can handle this properly.Then, this query will always return only one correct column info.
Extra
The default postgis srid value is 0, which means, if you omit the
Column
optionsrid
like this,postgis set this column's srid to 0.
However in
ColumnMetadata.ts
,srid value zero can't pass this if statement, so that migration process think there are changes.
Related issue
By fixing the condition of if statements like this
options.args.options.srid !== undefined
we can solve this problem.