Skip to content

SELECT Queries Strip Leading Underscore from Column Names #11423

Closed

Description

@todd

Issue Description

I have a model that includes a column wrapped in two underscores (__machina__). Performing a SELECT including this column appears to strip the leading underscore, which halts the query and throws an error. This worked in V4, so this appears to be a new issue in V5 (I'm on 5.18.4).

What are you doing?

const columns: ModelAttributes = {
  id: {
    type: DataTypes.INTEGER,
    autoIncrement: true,
    primaryKey: true
  },

  __machina__: {
    type: DataTypes.JSONB,
    allowNull: false
  }
};

class MyModel extends Model {
  public readonly id!: number;
  private __machina__!: any;
  public readonly created_at!: Date;
  public readonly updated_at!: Date;
}

export default (sequelize: Sequelize) => {
  MyModel.init(columns, {
    name: {
      singular: 'my_model',
      plural: 'my_models'
    },

    tableName: 'my_models',
    underscored: true,

    sequelize
  });

  return MyModel;
};

MyModel.findOne().then(console.log);

What do you expect to happen?

For the query to succeed and a record to be returned.

What is actually happening?

An error is thrown claiming no column exists for _machina__, which is the desired column stripped of its leading underscore. The query succeeds when the attribute is changed to ___machina__ (with three leading underscores), but then the attribute must be referenced as ___machina__, which breaks the library that's relying on this column. It appears no matter how many leading underscores are prefixed, the first is always removed.

> MyModel.findOne().then(i => instance = i)
Promise [Object] {
  _bitField: 0,
  _fulfillmentHandler0: undefined,
  _rejectionHandler0: undefined,
  _promise0: undefined,
  _receiver0: undefined }
> Executing (default): SELECT "id", "_machina__" AS "__machina__" FROM "my_models" AS "MyModel" LIMIT 1;
(node:50782) [DEP0097] DeprecationWarning: Using a domain property in MakeCallback is deprecated. Use the async_context variant of MakeCallback or the AsyncResource class instead.
Unhandled rejection SequelizeDatabaseError: column "_machina__" does not exist
    at Query.formatError ([REDACTED]node_modules/sequelize/lib/dialects/postgres/query.js:366:16)
    at query.catch.err ([REDACTED]node_modules/sequelize/lib/dialects/postgres/query.js:72:18)
    at bound (domain.js:402:14)
    at runBound (domain.js:415:12)
    at tryCatcher ([REDACTED]node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler ([REDACTED]node_modules/bluebird/js/release/promise.js:517:31)
    at Promise._settlePromise ([REDACTED]node_modules/bluebird/js/release/promise.js:574:18)
    at Promise._settlePromise0 ([REDACTED]node_modules/bluebird/js/release/promise.js:619:10)
    at Promise._settlePromises ([REDACTED]node_modules/bluebird/js/release/promise.js:695:18)
    at _drainQueueStep ([REDACTED]node_modules/bluebird/js/release/async.js:138:12)
    at _drainQueue ([REDACTED]node_modules/bluebird/js/release/async.js:131:9)
    at Async._drainQueues ([REDACTED]node_modules/bluebird/js/release/async.js:147:5)
    at Immediate.Async.drainQueues [as _onImmediate] ([REDACTED]node_modules/bluebird/js/release/async.js:17:14)
    at runCallback (timers.js:705:18)
    at tryOnImmediate (timers.js:676:5)
    at processImmediate (timers.js:658:5)
    at process.topLevelDomainCallback (domain.js:126:23)

Environment

  • Sequelize version: 5.18.4
  • Node.js version: 10.16.3
  • Operating System: MacOS
  • TypeScript version: 3.6.2

Issue Template Checklist

How does this problem relate to dialects?

  • I think this problem happens regardless of the dialect.
  • I think this problem happens only for the following dialect(s):
  • I don't know, I was using Postgres, with connector library version 7.12.1 and database version 9.16.0

Would you be willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time and I know how to start.
  • Yes, I have the time but I don't know how to start, I would need guidance.
  • No, I don't have the time, although I believe I could do it if I had the time...
  • No, I don't have the time and I wouldn't even know how to start.

Activity

  1. todd commented on Sep 11, 2019

    @todd
    ContributorAuthor

    Ok, tracked this down. Here's the relevant block: https://github.com/sequelize/sequelize/blob/master/lib/model.js#L1140-L1142

    Digging in deeper, it looks like the inflection library is stripping that leading underscore if underscored is set to true in your model definition. Setting a field override to the desired column name in the column definition got me back up and running, but I'm not sure this is the desired behavior, so I'm going to leave this open for now.

  2. papb commented on Sep 14, 2019

    @papb
    Member

    Definitely not the desired behavior! Thanks for the report. I will close this in favor of #11225

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions