Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support postgres column with varchar datatype and uuid_generate_v4() default #9063

Closed
knarfchan opened this issue Jun 2, 2022 · 1 comment · Fixed by #9065
Closed

Support postgres column with varchar datatype and uuid_generate_v4() default #9063

knarfchan opened this issue Jun 2, 2022 · 1 comment · Fixed by #9065

Comments

@knarfchan
Copy link
Contributor

knarfchan commented Jun 2, 2022

Feature Description

A new column option for generated called default that represents a column generated by whatever default function a column.

@Column({
    name: "some_uuid_column",
    default: () => "uuid_generate_v4()",
    generated: "default",
    type: "varchar",
    length: 256,
  })

The Problem

Right now there's no way for an Entity column to represent a column in Postgres that has varchar data type and uuid_generate_v4() default.

Typeorm thinks that any column with uuid_generate_v4() default should have generationStrategy uuid, and it thinks that anything with generationStrategy uuid should be of data type uuid. This causes Typeorm to constantly try to drop and re-create any column that tries to define a varchar column with default uuid_generate_v4() when generating a migration.

The Solution

I tried patching the Typeorm library with a new generated type called default and mapping non uuid columns with uuid_generate_v4() to this default. Then when defining the column, I can define it like below and this seems to work.

@Column({
    name: "some_uuid_column",
    default: () => "uuid_generate_v4()",
    generated: "default",
    type: "varchar",
    length: 256,
  })

Considered Alternatives

Is there anything special about a column marked as generated in this case? Might be better just to parse Postgres columns with varchar datatype and uuid_generate_v4 to have isGenerated = false. Happy to go this route if it makes sense and is simpler.

Additional Context

I need to match the database's column definition as is because it's a huge table and there multiple of them, so I need the typeorm column definition to conform to the column instead of vice versa.

Relevant Database Driver(s)

DB Type Relevant
aurora-mysql no
aurora-postgres no
better-sqlite3 no
cockroachdb no
cordova no
expo no
mongodb no
mysql no
nativescript no
oracle no
postgres yes
react-native no
sap no
spanner no
sqlite no
sqlite-abstract no
sqljs no
sqlserver no

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, but I can support (using donations) development.
  • ✖️ No, I don’t have the time and I’m okay to wait for the community / maintainers to resolve this issue.

Any guidance would be great! Thanks!

@knarfchan
Copy link
Contributor Author

The more I look into it, the more it makes sense to pursue the alternative.

@knarfchan knarfchan changed the title New generated option to support postgres column with varchar datatype and uuid_generate_v4() default Support postgres column with varchar datatype and uuid_generate_v4() default Jun 2, 2022
knarfchan added a commit to knarfchan/typeorm that referenced this issue Jun 2, 2022
AlexMesser added a commit that referenced this issue Aug 24, 2022
…efault (#9065)

* fix: support postgres column with varchar datatype and uuid_generate_v4() default

Closes: #9063

* added test

Co-authored-by: Dmitry Zotov <dmzt08@gmail.com>
wirekang pushed a commit to wirekang/typeorm that referenced this issue Aug 25, 2022
…efault (typeorm#9065)

* fix: support postgres column with varchar datatype and uuid_generate_v4() default

Closes: typeorm#9063

* added test

Co-authored-by: Dmitry Zotov <dmzt08@gmail.com>
nordinh pushed a commit to nordinh/typeorm that referenced this issue Aug 29, 2022
…efault (typeorm#9065)

* fix: support postgres column with varchar datatype and uuid_generate_v4() default

Closes: typeorm#9063

* added test

Co-authored-by: Dmitry Zotov <dmzt08@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant