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

Omit full table path in join query results. #7605

Closed
brettvitaz opened this issue May 4, 2017 · 9 comments
Closed

Omit full table path in join query results. #7605

brettvitaz opened this issue May 4, 2017 · 9 comments

Comments

@brettvitaz
Copy link
Contributor

brettvitaz commented May 4, 2017

I am trying to omit the full table path (e.g. tableB.column) from results in a raw query using a join. Is there a mechanism for doing this, or a workaround? I believe that this is related to issue #4311.

I have tried aliasing the column, but it still added the prefix.

What you are doing?

I am executing a query that looks something like the following:

  TableA
    .findAll({
      attributes: ['id', 'name', 'other'],
      include: [{
        attributes: ['partner_id'],
        model: TableB,
        nested: false,
        required: true,
      }],
      raw: true,
    })

What do you expect to happen?

I'd like to see output that does not include tableB and resembles:

[
  {
    "id": 1,
    "name": "stuff",
    "other": true,
    "partner_id": 1,
  }
]

What is actually happening?

I am getting output that resembles:

[
  {
    "id": 1,
    "name": "stuff",
    "other": true,
    "tableB.partner_id": 1,
  }
]

Dialect: postgres
Database version: 9.6.2
Sequelize version: 3.30.4

@sushantdhiman
Copy link
Contributor

With { raw: true } you will get whatever columns you have selected from database. You will have to loop through keys and remove prefix tableB yourself

@brettvitaz
Copy link
Contributor Author

I have already implemented this and was afraid that it would be your answer. I would like to request that columns with a column alias be given the option to be specified explicitly and honored even in a join. This becomes a problem when dealing with consumers that expect a certain payload, because part of an api must be written in a non-standard way to compensates for this.

@sushantdhiman
Copy link
Contributor

I agree, there should be a way to get aliased names, may be we can tackle it in #5708

@mjviljan
Copy link

mjviljan commented May 24, 2017

I've managed to get this working by including the joined attributes in the main attributes section with the table name included.

I.e. instead of

TableA
    .findAll({
      attributes: ['id', 'name', 'other'],
      include: [{
        attributes: ['partner_id'],
        model: TableB,
        nested: false,
        required: true,
      }],
      raw: true,
    })

you could try

TableA
    .findAll({
      attributes: ['id', 'name', 'other', 'TableB.partner_id'],
      include: [{
        attributes: [],
        model: TableB,
        nested: false,
        required: true,
      }],
      raw: true,
    })

I think the fact this works might not be explicitly intended behavior, however, and might change in the future. But at least for me it's currently working.

Edit: I'm using this against a MySQL database and my Sequelize version is 3.30.4.

@mortenko
Copy link

mortenko commented Nov 30, 2018

I have same problem. I'm using findAndCountAll method and it concat me table name to column name. So response is really "bad" if you are using it with graphql...
It does not work for :
db: postgresql
sequelize: 4.37.10

@odravison
Copy link

I've managed to get this working by including the joined attributes in the main attributes section with the table name included.

I.e. instead of

TableA
    .findAll({
      attributes: ['id', 'name', 'other'],
      include: [{
        attributes: ['partner_id'],
        model: TableB,
        nested: false,
        required: true,
      }],
      raw: true,
    })

you could try

TableA
    .findAll({
      attributes: ['id', 'name', 'other', 'TableB.partner_id'],
      include: [{
        attributes: [],
        model: TableB,
        nested: false,
        required: true,
      }],
      raw: true,
    })

I think the fact this works might not be explicitly intended behavior, however, and might change in the future. But at least for me it's currently working.

Edit: I'm using this against a MySQL database and my Sequelize version is 3.30.4.

I have been trying to do this approach and I can't get the same result.

Perhaps because Sequelize has changed?! Can you get the same result yet?

@jscamara
Copy link

jscamara commented Sep 2, 2020

Hi,

This should do the trick,

TableA.findAll({
  attributes: ['id', 'name', 'other', [Sequelize.col('TableB.partner_id'), 'partner_id']],
  raw: true,
  include: [{
    model: TableB,
    required: true
  }]
})

@odravison
Copy link

odravison commented Sep 2, 2020

Hi,

This should do the trick,

TableA.findAll({
  attributes: ['id', 'name', 'other', [Sequelize.col('TableB.partner_id'), 'partner_id']],
  raw: true,
  include: [{
    model: TableB,
    required: true
  }]
})

That's RIGHT dude, that's it!
This is what I'm looking for!

@kennethpdev
Copy link

kennethpdev commented Sep 30, 2020

I've managed to get this working by including the joined attributes in the main attributes section with the table name included.

I.e. instead of

TableA
    .findAll({
      attributes: ['id', 'name', 'other'],
      include: [{
        attributes: ['partner_id'],
        model: TableB,
        nested: false,
        required: true,
      }],
      raw: true,
    })

you could try

TableA
    .findAll({
      attributes: ['id', 'name', 'other', 'TableB.partner_id'],
      include: [{
        attributes: [],
        model: TableB,
        nested: false,
        required: true,
      }],
      raw: true,
    })

I think the fact this works might not be explicitly intended behavior, however, and might change in the future. But at least for me it's currently working.

Edit: I'm using this against a MySQL database and my Sequelize version is 3.30.4.

thanks for this!!

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

7 participants