# Postgresql enum type does not exist error - wrong enum type name

**URL:** <https://community.forestadmin.com/t/postgresql-enum-type-does-not-exist-error-wrong-enum-type-name/5931>\
**Category:** Help me!\
**Created:** [January 31, 2023, 10:44am UTC](https://community.forestadmin.com/t/postgresql-enum-type-does-not-exist-error-wrong-enum-type-name/5931 "2023-01-31T10:44:03Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cyrille](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/cyrille/32/5709_2.png) [@Cyrille](https://community.forestadmin.com/u/Cyrille)\
**Post date:** [January 31, 2023, 10:44am UTC](https://community.forestadmin.com/t/postgresql-enum-type-does-not-exist-error-wrong-enum-type-name/5931/1 "2023-01-31T10:44:03Z")

</div>

## Feature(s) impacted

Add new record on a table

## Observed behavior

Not able to add a new record on a table due to a “type xxxxx does not exist” error.  
Actually this type does not exist on our database but it’s also not that one which should be used.  
Forestadmin backend is looking for a type named “enum\_token\_account\_access\_permissions” while on our database the type is called “token\_account\_access\_permissions\_enum”.  
It seems to be only when we try to insert data as when displayed values of this type are correct.

## Expected behavior

Be able to add a new record

## Failure Logs

 ![image(1)](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/4/4067eb67d887aa055ffc4a23ab7c33a596559d4b.png)

## Context

- Project name: Tokeny
- Team name: admin
- Environment name: develop
- Agent type & version: Agent v2

---

<div class="post-metadata">

**Author:** ![anon39940173](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@anon39940173](https://community.forestadmin.com/u/anon39940173)\
**Post date:** [January 31, 2023, 10:54am UTC](https://community.forestadmin.com/t/postgresql-enum-type-does-not-exist-error-wrong-enum-type-name/5931/2 "2023-01-31T10:54:30Z")

</div>

Hi @Cyrille

Which driver are you using? `@forestadmin/datasource-sql` or `@forestadmin/datasource-sequelize` ?

- If using `datasource-sql`, can you provide us the `create table` statement
- If using `datasource-sequelize`, can you provide us with the `create table` statement + sequelize model file + sequelize version

That should allow us to reproduce, and issue a fix if relevant

---

<div class="post-metadata">

**Author:** ![Cyrille](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/cyrille/32/5709_2.png) [@Cyrille](https://community.forestadmin.com/u/Cyrille)\
**Post date:** [January 31, 2023, 1:05pm UTC](https://community.forestadmin.com/t/postgresql-enum-type-does-not-exist-error-wrong-enum-type-name/5931/4 "2023-01-31T13:05:47Z")

</div>

@anon39940173 thanks for your answer, we are using `@forestadmin/datasource-sql` but there is no `create table` statement. At that stage we try to insert a new row on an existing table (from forestadmin app, click on `+ Add` button

---

<div class="post-metadata">

**Author:** ![anon39940173](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@anon39940173](https://community.forestadmin.com/u/anon39940173)\
**Post date:** [January 31, 2023, 2:13pm UTC](https://community.forestadmin.com/t/postgresql-enum-type-does-not-exist-error-wrong-enum-type-name/5931/5 "2023-01-31T14:13:44Z")

</div>

I meant, can you provide me with a `create table` statement so that I can create the same table on a local database (on my machine) and try to reproduce the issue?

You can get those on most postgres GUI, or using pg dump

> **[PostgreSQL - How to generate CREATE TABLE query for an existing table?](https://tableplus.com/blog/2018/07/postgresql-how-to-copy-table-creation-statement-of-existing-table.html)**
>
> Generate table creation statement for an existing table

---

<div class="post-metadata">

**Author:** ![Cyrille](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/cyrille/32/5709_2.png) [@Cyrille](https://community.forestadmin.com/u/Cyrille)\
**Post date:** [January 31, 2023, 2:38pm UTC](https://community.forestadmin.com/t/postgresql-enum-type-does-not-exist-error-wrong-enum-type-name/5931/6 "2023-01-31T14:38:20Z")

</div>

sorry I misunderstood, here is the create table statement :

```javascript
CREATE TABLE test.access (
    account_id character varying NOT NULL,
    token_id character varying NOT NULL,
    agent character varying,
    provider character varying DEFAULT 'USER_MANAGED'::character varying NOT NULL,
    permissions test.token_account_access_permissions_enum[] NOT NULL
);

```

---

<div class="post-metadata">

**Author:** ![anon39940173](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@anon39940173](https://community.forestadmin.com/u/anon39940173)\
**Post date:** [February 1, 2023, 9:09am UTC](https://community.forestadmin.com/t/postgresql-enum-type-does-not-exist-error-wrong-enum-type-name/5931/7 "2023-02-01T09:09:24Z")

</div>

As a follow-up, I’m replicating the issue:

![image](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/4/4d56e7b3c57c50d48a6cf8b459c5a576ad7b26d1.png)

I’m investigating to find a fix

---

<div class="post-metadata">

**Author:** ![anon39940173](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@anon39940173](https://community.forestadmin.com/u/anon39940173)\
**Post date:** [February 1, 2023, 10:50am UTC](https://community.forestadmin.com/t/postgresql-enum-type-does-not-exist-error-wrong-enum-type-name/5931/8 "2023-02-01T10:50:05Z")

</div>

We use sequelize under the hood in the `@forestadmin/datasource-sql` so that it is feasible for us to support most database vendors without maintaining separate codebases.

The issue comes from the fact that when using sequelize’s `enum` type, the ORM assumes that the name of the custom type in postgres is `enum_${tableName}_${columnName}` (which is the case when the database is initially constructed with sequelize).

I’m working on checking if I can find a fix, but honestly, I don’t think it will get done today. This will most likely involve creating a custom type in sequelize, which will take some time to write / review / release on our side.

As a workaround, would you consider renaming your enum?

```javascript
ALTER TYPE old_type_name RENAME TO new_type_name;

```

Links to the failing code in sequelize for reference:

- [generation of the failing SQL code](https://github.com/sequelize/sequelize/blob/fd4afa6a89c111c6d6d0c94f0b98bf421b5357b6/src/utils.js#L555)
- [generation of enum names](https://github.com/sequelize/sequelize/blob/v6.28.0/src/dialects/postgres/data-types.js#L487)

---

<div class="post-metadata">

**Author:** ![anon39940173](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@anon39940173](https://community.forestadmin.com/u/anon39940173)\
**Post date:** [February 1, 2023, 1:49pm UTC](https://community.forestadmin.com/t/postgresql-enum-type-does-not-exist-error-wrong-enum-type-name/5931/9 "2023-02-01T13:49:00Z")

</div>

Made a draft of a PR that fixes the issue.  
Adding tests + reviewing this should take a couple of days, but at least it is fixable 🙂

I’ll update the thread once it is done

> <https://github.com/ForestAdmin/agent-nodejs/pull/616>
>
> \## Definition of Done
> 
> \### General
> 
> \- \[\] Write an explicit title for the Pu…ll Request, following \[Conventional Commits specification\](https://www.conventionalcommits.org)
> \- \[\] Test manually the implemented changes
> \- \[\] Validate the code quality (indentation, syntax, style, simplicity, readability)
> 
> \### Security
> 
> \- \[\] Consider the security impact of the changes made

---

<div class="post-metadata">

**Author:** ![Cyrille](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/cyrille/32/5709_2.png) [@Cyrille](https://community.forestadmin.com/u/Cyrille)\
**Post date:** [February 1, 2023, 1:55pm UTC](https://community.forestadmin.com/t/postgresql-enum-type-does-not-exist-error-wrong-enum-type-name/5931/10 "2023-02-01T13:55:23Z")

</div>

thanks a lot for your help

---

<div class="post-metadata">

**Author:** ![anon39940173](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@anon39940173](https://community.forestadmin.com/u/anon39940173)\
**Post date:** [February 9, 2023, 9:04am UTC](https://community.forestadmin.com/t/postgresql-enum-type-does-not-exist-error-wrong-enum-type-name/5931/11 "2023-02-09T09:04:57Z")

</div>

Hi @Cyrille

It took more time than expected, but I finally found the time to merge [the fix](https://github.com/ForestAdmin/agent-nodejs/pull/616) this morning.

Can you upgrade all `@forestadmin/` packages that you are using and tell me if everything works?

---

<div class="post-metadata">

**Author:** ![Cyrille](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/cyrille/32/5709_2.png) [@Cyrille](https://community.forestadmin.com/u/Cyrille)\
**Post date:** [February 9, 2023, 9:48am UTC](https://community.forestadmin.com/t/postgresql-enum-type-does-not-exist-error-wrong-enum-type-name/5931/12 "2023-02-09T09:48:08Z")

</div>

thanks a lot @anon39940173 I’ll deploy the fix asap and keep you updated

---

<div class="post-metadata">

**Author:** ![Cyrille](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/cyrille/32/5709_2.png) [@Cyrille](https://community.forestadmin.com/u/Cyrille)\
**Post date:** [February 9, 2023, 10:55am UTC](https://community.forestadmin.com/t/postgresql-enum-type-does-not-exist-error-wrong-enum-type-name/5931/13 "2023-02-09T10:55:01Z")

</div>

Hi @anon39940173 I’ve tested the latest version of packages and I still have an error : from what I can see the enum name is now correct but I think the schema name is missing and forest is not able to retrieve properly the enum (it’s looking for `token_account_access_permissions_enum[]` instead of `servicing.token_account_access_permissions_enum[]` (not 100% sure)

 ![Screenshot 2023-02-09 at 11.53.46](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/b/bef4e0594f077837878dd799e85683778adb442e.jpeg)

---

<div class="post-metadata">

**Author:** ![matthieu](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/matthieu/32/4834_2.png) [@matthieu](https://community.forestadmin.com/u/matthieu)\
**Post date:** [February 9, 2023, 11:18am UTC](https://community.forestadmin.com/t/postgresql-enum-type-does-not-exist-error-wrong-enum-type-name/5931/14 "2023-02-09T11:18:04Z")

</div>

Hi !

I also encounter the same issue as I’m using Prisma.  
I tried the latest version and there’s a remaining issue linked to casing.  
If the enum name contains uppercase characters, the name has to be quoted to prevent it from being automatically lowercased by Postgres.

I was able to make it work by applying this patch :

```javascript
diff --git a/node_modules/@forestadmin/datasource-sql/dist/orm-builder/helpers/sequelize-type.js b/node_modules/@forestadmin/datasource-sql/dist/orm-builder/helpers/sequelize-type.js
index 8a8561e..671dd37 100644
--- a/node_modules/@forestadmin/datasource-sql/dist/orm-builder/helpers/sequelize-type.js
+++ b/node_modules/@forestadmin/datasource-sql/dist/orm-builder/helpers/sequelize-type.js
@@ -40,7 +40,7 @@ class SequelizeTypeFactory {
                 constructor() {
                     super();
                     this.isDataSourceSqlEnum = true;
- this.key = name;
+ this.key = '"'+name+'"';
                     // Steal the validate method from the ENUM type
                     this.validate = sequelize_1.DataTypes.ENUM.prototype.validate;
                     this.values = values;
@@ -54,7 +54,7 @@ class SequelizeTypeFactory {
             // Setting this tells sequelize the name of the type in the database.
             // This is used, most notably, when casting values (which happens when the enum is used in
             // arrays)
- _a.key = name,
+ _a.key = '"'+name+'"',
             _a);
         return new Type();
     }

```

Though I’m not sure if it’s the proper place to make this fix.

---

<div class="post-metadata">

**Author:** ![anon39940173](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@anon39940173](https://community.forestadmin.com/u/anon39940173)\
**Post date:** [February 9, 2023, 2:12pm UTC](https://community.forestadmin.com/t/postgresql-enum-type-does-not-exist-error-wrong-enum-type-name/5931/15 "2023-02-09T14:12:31Z")

</div>

Thank you to both of you about the super detailed feedback and sorry for both the long delay in handling this, and the badly tested fix.

I’ll come back to you with something better shortly 😅

---

<div class="post-metadata">

**Author:** ![anon39940173](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@anon39940173](https://community.forestadmin.com/u/anon39940173)\
**Post date:** [February 9, 2023, 3:14pm UTC](https://community.forestadmin.com/t/postgresql-enum-type-does-not-exist-error-wrong-enum-type-name/5931/16 "2023-02-09T15:14:56Z")

</div>

Quick follow up: a pull-request is in review

> <https://github.com/ForestAdmin/agent-nodejs/pull/624>
>
> \## Definition of Done
> 
> \### General
> 
> \- \[\] Write an explicit title for the Pu…ll Request, following \[Conventional Commits specification\](https://www.conventionalcommits.org)
> \- \[\] Test manually the implemented changes
> \- \[\] Validate the code quality (indentation, syntax, style, simplicity, readability)
> 
> \### Security
> 
> \- \[\] Consider the security impact of the changes made

---

<div class="post-metadata">

**Author:** ![anon39940173](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@anon39940173](https://community.forestadmin.com/u/anon39940173)\
**Post date:** [February 9, 2023, 3:31pm UTC](https://community.forestadmin.com/t/postgresql-enum-type-does-not-exist-error-wrong-enum-type-name/5931/17 "2023-02-09T15:31:17Z")

</div>

And I got someone to review it in a timely fashion.  
The CI/CD is currently publishing the new version in NPM.

Can you try again with the latest version in 10ish minutes?

---

<div class="post-metadata">

**Author:** ![Cyrille](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/cyrille/32/5709_2.png) [@Cyrille](https://community.forestadmin.com/u/Cyrille)\
**Post date:** [February 9, 2023, 4:23pm UTC](https://community.forestadmin.com/t/postgresql-enum-type-does-not-exist-error-wrong-enum-type-name/5931/18 "2023-02-09T16:23:25Z")

</div>

it’s now working on my side, thanks again a lot for your help!

---

<div class="post-metadata">

**Author:** ![matthieu](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/matthieu/32/4834_2.png) [@matthieu](https://community.forestadmin.com/u/matthieu)\
**Post date:** [February 9, 2023, 4:50pm UTC](https://community.forestadmin.com/t/postgresql-enum-type-does-not-exist-error-wrong-enum-type-name/5931/19 "2023-02-09T16:50:11Z")

</div>

It’s working on my end as well!

Thanks @anon39940173!

---

<div class="post-metadata">

**Author:** ![anon39940173](https://avatars.discourse-cdn.com/v4/letter/a/8c91f0/32.png) [@anon39940173](https://community.forestadmin.com/u/anon39940173)\
**Post date:** [February 10, 2023, 8:45am UTC](https://community.forestadmin.com/t/postgresql-enum-type-does-not-exist-error-wrong-enum-type-name/5931/20 "2023-02-10T08:45:18Z")

</div>

That’s great

Thank you to both of you for providing well documented and reproducible issues
