balderdashy / waterline-sequel

A SQL generator for use in Waterline Adapters
MIT License
16 stars 61 forks source link

PostgreSQL error when aggregate and group by custom column with camel-cased columnName #74

Open masitko opened 8 years ago

masitko commented 8 years ago

There is a problem when using PostgreSQL and trying to aggregate and group data by column with custom camel-cased columnName. For an example:

module.exports = {
  schema: true,
  attributes: {
    ip: {
      type: 'string',
    },
    host: {
      type: 'string',
    },
    count: {
      type: 'integer',
      defaultsTo: 1
    },
    user: {
      model: 'User',
      columnName: 'userId',
    }
  }
};

If we try to group users by userId column:

 UserLogin.find().sum('count').groupBy('userId')

created statement will trigger two errors in PostgreSQL:

SELECT userId as group0, CAST(SUM("userlogin"."count") AS float) AS count FROM "public"."userlogin" AS "userlogin"   GROUP BY userId

as we can see userId is missing required in this case double quotes.

particlebanana commented 8 years ago

@masitko in this case what is the outcome when running:

UserLogin.find().sum('count').groupBy('user')

The point of the columnName is so that the mapping on the backend can perform this for you. You should only use the defined attributes you supply in queries.

masitko commented 8 years ago

@particlebanana This is the query produced by your statement:

SELECT userId as group0, CAST(SUM("userlogin"."count") AS float) AS count FROM "public"."userlogin" AS "userlogin"   GROUP BY user

Column was created properly with requested name 'userId' I'm using latest releases of sails-postgresql and waterline-sequel