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

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

Context

  • Project name: Tokeny
  • Team name: admin
  • Environment name: develop
  • Agent type & version: Agent v2
1 Like

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

@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

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

sorry I misunderstood, here is the create table statement :

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
);

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

image

I’m investigating to find a fix

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?

ALTER TYPE old_type_name RENAME TO new_type_name;

Links to the failing code in sequelize for reference:

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 :slight_smile:

I’ll update the thread once it is done

2 Likes

thanks a lot for your help

Hi @Cyrille

It took more time than expected, but I finally found the time to merge the fix this morning.

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

1 Like

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

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)

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 :

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.

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 :sweat_smile:

Quick follow up: a pull-request is in review

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?

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

It’s working on my end as well!

Thanks @anon39940173!

That’s great

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