k0stya
k0stya

Reputation: 4315

Sequelize generates some random alias in production build

The following query works just fine on local env with dev build (next.js app) though starts generating some random alias for Payments table when executed on prod build (deployed using Vercel). Basically, Sequelize generates a query with as "gs" for Payments table.

const existingBooking = await BookingRequests.findByPk(id, {
   include: [
     { model: SectorBookings, include: [Payments] },
     { model: BookingComments, attributes: ["text"] },
   ],
 });

There is many-to-many association between Payments and SectorBookings

SectorBookings.belongsToMany(Payments, {
  through: SectorBookingPayments,
  foreignKey: "sectorBookingId",
  otherKey: "paymentId",
});
Payments.belongsToMany(SectorBookings, {
  through: SectorBookingPayments,
  foreignKey: "paymentId",
  otherKey: "sectorBookingId",
});

Upvotes: 1

Views: 286

Answers (1)

Ballsacian1
Ballsacian1

Reputation: 17322

I have a similar issue when using [email protected] but goes away if I use [email protected].

I haven't determined exactly why this is the case yet, but my intuition is that the minifier is doing something strange.

EDIT w/ Solutions

I found the solution and the most likely reason this is happening. It looks like the swc minifier is minifying the class names of sequelize models. Sequelize by default uses the class name for determining the modelName which is what is used by default in queries.

Solution:

Every model class created in sequelize should have modelName directly defined. That way the class name has no bearing on the final name used inside of sequelize queries.

Here is a sample configuration that shows the change necessary

class TestModel extends Model<InferAttributes<TestModel>, InferCreationAttributes<TestModel>> {
  declare id: CreationOptional<number>;
  declare title: string;
}

TestModel.init(
  {
    id: {
      type: DataTypes.INTEGER,
      autoIncrement: true,
      primaryKey: true
    },
    title: {
      type: DataTypes.STRING,
      allowNull: false
    }
  },
  {
    // THIS IS THE FIX
    modelName: 'TestModel',
    sequelize
  }
);

Upvotes: 2

Related Questions