0

I’m newish to Adminjs and I’m having the following challenge: I have one table with both id and name columns. Adminjs automatically shows the name value for columns that are referenced with a foreign key to the id of the parent table. (Great feature!)

I have another foreign key relationship to a parent table that has a column labeled proper_name instead of just name. I would love to recreate this table display behavior with the proper_name column values being displayed instead of just displaying the parent table foreign key id values when referencing the foreign key related table.

Is there a way to customize/override this display behavior so the dropdown shows the value of another column from the parent table other than the id column?

parent table model:

class parent_1_table extends Model {
    static associate(models) {
    }
  }
  parent_table.init({
    id: {
      type: Sequelize.BIGINT,
      primaryKey: true,
      autoIncrement: true,
    },
    proper_name: {
      type: Sequelize.STRING(32),
      isTitle: true,
    },
  }, {
    sequelize,
    modelName: 'parent_table',
    timestamps: false,
    createdAt: false,
    updatedAt: false,
    freezeTableName: true,
    tableName: 'parent_table',
  });

child table:

class child_table extends Model {
    static associate(models) {
    }
  }
  child_table.init({
    parent_1_id: {
      type: Sequelize.BIGINT,
      allowNull: false,
    },
    parent_2_id: {
      type: Sequelize.BIGINT,
      allowNull: false,
    },
  }, {
    sequelize,
    modelName: 'child_table',
    timestamps: false,
    createdAt: false,
    updatedAt: false,
    freezeTableName: true,
    tableName: 'child_table',
  });

foreign key relationships are defined after the fact in the index.js file:

child_table.belongsTo(parent_1_table, { foreignKey: 'parent_1_id' });
child_table.belongsTo(parent_2, { foreignKey: 'parent_2_id' });
NylonEric
  • 11
  • 4
  • I tried a potential solution from one of the developers/contributors but it didn't solve it for me: `You can define your title property by adding isTitle: true in the property which you want to use as title (in your referenced resource options):` https://github.com/SoftwareBrothers/adminjs/issues/1132#issuecomment-1123749783 – NylonEric Jun 26 '22 at 22:37

1 Answers1

1

One of the project contributors in the community Discord clarified the above isTitle: true solution needs to be AFTER the column definitions in the options area:

class parent_1_table extends Model {
    static associate(models) {
    }
  }
  parent_table.init({
    id: {
      type: Sequelize.BIGINT,
      primaryKey: true,
      autoIncrement: true,
    },
    proper_name: {
      type: Sequelize.STRING(32),
      // isTitle: true, * not here
    },
  }, {
    sequelize,
    properties: { // put these display options here!
      proper_name: {
        isTitle: true,
      },
    },
    modelName: 'parent_table',
    timestamps: false,
    createdAt: false,
    updatedAt: false,
    freezeTableName: true,
    tableName: 'parent_table',
  });
NylonEric
  • 11
  • 4