typeorm: TypeError: Cannot read property 'map' of undefined in PostgresQueryRunner.createEnumTypeSql
Issue type: bug report
Database system/driver: postgres
TypeORM version: 0.2.6 as reported by
$ typeorm version
Local installed version: 0.2.6
Global installed TypeORM version: 0.2.6
Steps to reproduce or a small repository showing the problem:
The cause seems to be this entity:
enum State {
Running = 0,
Completed = 1,
}
@Entity('presence_list_item')
export class Presence {
@PrimaryGeneratedColumn('uuid')
public id!: string
@Column('enum', {nullable: false})
public state!: State
}
And TypeORM dies when I attempt to sync the schema:
$ typeorm schema:sync
query: START TRANSACTION
query: SELECT * FROM current_schema()
query: SELECT * FROM "information_schema"."tables" WHERE ...
query: SELECT "ns"."nspname" AS "table_schema", "t"."relname" AS "table_name", "cnst"."conname" AS "constraint_name", "cnst"."consrc" AS "expression", CASE "cnst"."contype" WHEN 'p' THEN 'PRIMARY' WHEN 'u' THEN 'UNIQUE' WHEN 'c' THEN 'CHECK' END AS "constraint_type", "a"."attname" AS "column_name" FROM "pg_constraint" "cnst" INNER JOIN "pg_class" "t" ON "t"."oid" = "cnst"."conrelid" INNER JOIN "pg_namespace" "ns" ON "ns"."oid" = "cnst"."connamespace" INNER JOIN "pg_attribute" "a" ON "a"."attrelid" = "cnst"."conrelid" AND "a"."attnum" = ANY ("cnst"."conkey") WHERE ...
query: SELECT "ns"."nspname" AS "table_schema", "t"."relname" AS "table_name", "i"."relname" AS "constraint_name", "a"."attname" AS "column_name", CASE "ix"."indisunique" WHEN 't' THEN 'TRUE' ELSE'FALSE' END AS "is_unique", pg_get_expr("ix"."indpred", "ix"."indrelid") AS "condition" FROM "pg_class" "t" INNER JOIN "pg_index" "ix" ON "ix"."indrelid" = "t"."oid" INNER JOIN "pg_attribute" "a" ON "a"."attrelid" = "t"."oid" AND "a"."attnum" = ANY ("ix"."indkey") INNER JOIN "pg_namespace" "ns" ON "ns"."oid" = "t"."relnamespace" INNER JOIN "pg_class" "i" ON "i"."oid" = "ix"."indexrelid" LEFT JOIN "pg_constraint" "cnst" ON "cnst"."conname" = "i"."relname" WHERE ...
query: SELECT "con"."conname" AS "constraint_name", "con"."nspname" AS "table_schema", "con"."relname" AS "table_name", "att2"."attname" AS "column_name", "ns"."nspname" AS "referenced_table_schema", "cl"."relname" AS "referenced_table_name", "att"."attname" AS "referenced_column_name", "con"."confdeltype" AS "on_delete", "con"."confupdtype" AS "on_update" FROM ( SELECT UNNEST ("con1"."conkey") AS "parent", UNNEST ("con1"."confkey") AS "child", "con1"."confrelid", "con1"."conrelid", "con1"."conname", "con1"."contype", "ns"."nspname", "cl"."relname", CASE "con1"."confdeltype" WHEN 'a' THEN 'NO ACTION' WHEN 'r' THEN 'RESTRICT' WHEN 'c' THEN 'CASCADE' WHEN 'n' THEN 'SET NULL' WHEN 'd' THEN 'SET DEFAULT' END as "confdeltype", CASE "con1"."confupdtype" WHEN 'a' THEN 'NO ACTION' WHEN 'r' THEN 'RESTRICT' WHEN 'c' THEN 'CASCADE' WHEN 'n' THEN 'SET NULL' WHEN 'd' THEN 'SET DEFAULT' END as "confupdtype" FROM "pg_class" "cl" INNER JOIN "pg_namespace" "ns" ON "cl"."relnamespace" = "ns"."oid" INNER JOIN "pg_constraint" "con1" ON "con1"."conrelid" = "cl"."oid" WHERE ...
...
creating a new table: presence_list_item
query: SELECT "n"."nspname", "t"."typname" FROM "pg_type" "t" INNER JOIN "pg_namespace" "n" ON "n"."oid" = "t"."typnamespace" WHERE "n"."nspname" = current_schema() AND "t"."typname" = 'presence_list_item_state_enum'
query: ROLLBACK
Error during schema synchronization:
TypeError: Cannot read property 'map' of undefined
at PostgresQueryRunner.createEnumTypeSql (~/lib/node_modules/typeorm/driver/postgres/PostgresQueryRunner.js:2046:38)
at PostgresQueryRunner.<anonymous> (~/lib/node_modules/typeorm/driver/postgres/PostgresQueryRunner.js:447:69)
at step (~/lib/node_modules/typeorm/driver/postgres/PostgresQueryRunner.js:42:23)
at Object.next (~/lib/node_modules/typeorm/driver/postgres/PostgresQueryRunner.js:23:53)
at fulfilled (~/lib/node_modules/typeorm/driver/postgres/PostgresQueryRunner.js:14:58)
at process._tickCallback (internal/process/next_tick.js:68:7)
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 5
- Comments: 20 (3 by maintainers)
Commits related to this issue
- added validation for `enum` property (#2233) — committed to typeorm/typeorm by AlexMesser 3 years ago
- fix: fixed all known enum issues (#7419) * fix #5371 * fix #6471; fix: `enumName` changes not handled; fix: `enumName` does not handle table schema; * fixed falling test; * added test for ... — committed to typeorm/typeorm by AlexMesser 3 years ago
- Chore/flowaccount update (#2) * docs: fix small typo on package.json script example (#7408) Add missing colon in JSON property at `package.json` `"script"` example * feat: output Javascript Mig... — committed to flowaccount/typeorm by gogotaro 3 years ago
you have to provide enum this way:
I don’t think that what you provide here is a fix. You just show a correct declaration for a property that should map to an enumeration (the same way @pleerock did in the second comment). However, what was asked for (in my answer to that comment) is an improved error message in case the declaration is erroneous.
To make it even more clear, IMO a fix would be a patch to the TypeORM codebase that analyzes enum declarations more thoroughly and presents a more insightful error message as to why the declaration is not accepted.
Any update on this issue? I got the same error with @GitStorageOne
I had this same problem and was able to resolve it by making sure I defined the TypeScript enum in the same file as the entity definition that uses it, instead of trying to import the TypeScript enum from an external file.
If someone else is facing this issue using Nest.js, remove dist folder generated when you run the Nest application. This should erase any other migrations generated when typeorm module has synchronization set to true.
Guys, are there any known solutions for this? This kind of cr%p definitely hits scalability badly. I’ve hit the case when I have a generic
Stateenum type which is shared across several entities, there is no “single file”, where the enum can be put to next to the entity using it.this issue should reopen, hitting the error when enum is not defined in the same file
Same experience as @jldillman – any idea why the location of the enum declaration would matter?
I’ve tried to make the enum column a primary key and I’m getting this error, so basically
works fine however
and
do not
@smonv i was able to fix like this
Oh, I see. Sorry then. Maybe you could detect this error and inform users like me with a nicer error message? That would be super nice.