Royi Freifeld
Royi Freifeld

Reputation: 659

TypeORM migrations wrong syntax when using PostgreSQL and isGenerated

I'm trying to modify a TypeOrm migration script, already running perfectly with MySQL, to work with PostgreSQL.
I got stuck on some table creation with a generated column:

{
  name: 'id',
  type: 'varchar',
  isPrimary: true,
  isGenerated: true,
  generationStrategy: 'uuid',
}

Not sure why, but I get the following syntax (I left out the working parts):

CREATE TABLE "my_table_name" ("id" NOT NULL DEFAULT uuid_generate_v4(), <some other fields> , CONSTRAINT "<pk>" PRIMARY KEY ("id"))

The migration throws the following error: error: syntax error at or near "NOT", which I narrowed down to the obvious problem of no type declaration between "id" and NOT

If I remove the isGenerated from the config, the type appears and everything works but without the DEAFAULT uuid_generate_v4 part.

I'm not sure what could be the problem, and I'd appreciate your help

Thnx!

Upvotes: 2

Views: 1262

Answers (2)

Luiz Alexandre Silva
Luiz Alexandre Silva

Reputation: 31

Your id type must be 'uuid' to match generation strategy, like this:

{
  name: 'id',
  type: 'uuid',
  isPrimary: true,
  isGenerated: true,
  generationStrategy: 'uuid',
}

Upvotes: 3

Vagner Wentz
Vagner Wentz

Reputation: 566

When we use MYSQL we need to know that does not exist NOT NULL, exist IS NOT NULL. So your code will be CREATE TABLE "my_table_name" ("id" IS NOT NULL DEFAULT uuid_generate_v4(), <some other fields> , CONSTRAINT "<pk>" PRIMARY KEY ("id")).

https://www.techonthenet.com/mysql/is_not_null.php#:~:text=Example%20%2D%20With%20SELECT%20Statement,not%20contain%20a%20null%20value.

Upvotes: 0

Related Questions