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

Cannot create a record whose PK isn't autoincrement #110

Open
fabiosantoscode opened this issue Dec 14, 2022 · 0 comments
Open

Cannot create a record whose PK isn't autoincrement #110

fabiosantoscode opened this issue Dec 14, 2022 · 0 comments

Comments

@fabiosantoscode
Copy link

Hello there! I'm using adminJS together with sequelize, and I have to say, thanks for saving me from tedious admin code! ❤️

I have a country table with a countryCode PK that I'm not able to fill in using adminjs.

I understand it shouldn't be editable (it's a primary key after all) but I noticed that creating a new country resulted in a SQL query that didn't have a countryCode.

Here are the relevant bits of my Country model:

class Country extends Sequelize.Model {
  static init(sequelize, DataTypes) {
    return super.init(
      {
        countryCode: {
          type: DataTypes.CHAR(3),
          allowNull: false,
          primaryKey: true,
        },
        // ...
      },
      {
        // ...
        indexes: [
          {
            name: 'PRIMARY',
            unique: true,
            using: 'BTREE',
            fields: [{ name: 'countryCode' }],
          },
        ],
      }
    )
  }
}

After some digging, I noticed that in property.ts there is a isEditable function that returns false if the field is a primary key.

I fixed the issue using patch-package, by making isEditable not check isId during creation. The patch is below:


diff --git a/node_modules/@adminjs/sequelize/src/property.ts b/node_modules/@adminjs/sequelize/src/property.ts
index a135452..b586e15 100644
--- a/node_modules/@adminjs/sequelize/src/property.ts
+++ b/node_modules/@adminjs/sequelize/src/property.ts
@@ -43,14 +43,14 @@ class Property extends BaseProperty {
     return this.fieldName;
   }
 
-  isEditable(): boolean {
+  isEditable({ forCreate }: { forCreate?: boolean } = {}): boolean {
     if ((this.sequelizePath as any)._autoGenerated) {
       return false;
     }
     if (this.sequelizePath.autoIncrement) {
       return false;
     }
-    if (this.isId()) {
+    if (!forCreate && this.isId()) {
       return false;
     }
     return true;
diff --git a/node_modules/@adminjs/sequelize/src/resource.ts b/node_modules/@adminjs/sequelize/src/resource.ts
index e36a0e0..89562c1 100644
--- a/node_modules/@adminjs/sequelize/src/resource.ts
+++ b/node_modules/@adminjs/sequelize/src/resource.ts
@@ -156,7 +156,7 @@ class Resource extends BaseResource {
   }
 
   async create(params): Promise<Record<string, any>> {
-    const parsedParams = this.parseParams(params);
+    const parsedParams = this.parseParams(params, { forCreate: true });
     const unflattedParams = flat.unflatten<any, any>(parsedParams);
     try {
       const record = await this.SequelizeModel.create(unflattedParams);
@@ -215,7 +215,7 @@ class Resource extends BaseResource {
    *
    * @return  {Object}          converted params
    */
-  parseParams(params) {
+  parseParams(params, { forCreate }: { forCreate?: boolean } = {}) {
     const parsedParams = { ...params };
     this.properties().forEach((property) => {
       const value = parsedParams[property.name()];
@@ -224,7 +224,7 @@ class Resource extends BaseResource {
           delete parsedParams[property.name()];
         }
       }
-      if (!property.isEditable()) {
+      if (!property.isEditable({ forCreate })) {
         delete parsedParams[property.name()];
       }
     });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant