question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

results of raw query (with include) hard to use, due to table-aliases

See original GitHub issue

Column aliases specified by the user as part of the attributes section(s) (fooname, barname) are getting prefixed by the table aliases. This makes it harder to use them. (and requires knowledge what the alias will be like)

var S = require('sequelize');

var sequelize = new S('seq_test', 'passwd', '', {
    host: 'localhost',
    dialect: 'postgres',
    pool: { max: 15, min: 0, idle: 10000, },
    define: {  },
});

var User = sequelize.define('User', {
    name: { type: S.STRING(50), allowNull: true},
    pass: { type: S.STRING(50), allowNull: true},
});
var Foo = sequelize.define('Foo', {
    login: { type: S.STRING(50), allowNull: true},
    pass: { type: S.STRING(50), allowNull: true},
});
var Bar = sequelize.define('Bar', {
    login: { type: S.STRING(50), allowNull: true},
    pass: { type: S.STRING(50), allowNull: true},
});

User.belongsTo(Foo, {});
Foo.belongsTo(Bar, {});

sequelize.sync()
.then(function (){
    return User.create({name: 'A'});
})
.then(function (U){
    return Foo.create({login: 'B'})
    .then(function (F){
        return U.setFoo(F)
        .then(function (){
            return F;
        })
    });
})
.then(function (F){
    return Bar.create({login: 'C'})
    .then(function (B){
        return F.setBar(B);
    });
})
.then(function (){
    return User.findAll({
        raw: true,
        attributes: [ 
            [ S.col('name'), 'username' ] 
        ],
        include: [
            {   model: Foo,
                attributes: [ [ S.col('login'), 'fooname' ] ],
                raw: true, // workaruond issue 4308
                include: [
                    {   model: Bar,
                        attributes: [ [ S.col('login'), 'barname' ] ],
                    },
                ],
            },
        ],
    });
})
.then(function (rows){
    console.log(rows);
    rows.forEach(function(r){
        console.log('fooname: ',r['fooname'], 'Foo.fooname: ',r['Foo.fooname'], 'fooname: ',r['barname'], 'Foo.Bar.barname: ',r['Foo.Bar.barname']);
    });
});

runs the sql

 SELECT 
   "name" AS "username", 
   "Foo"."login" AS "Foo.fooname", 
   "Foo.Bar"."login" AS "Foo.Bar.barname" 
FROM "Users" AS "User" LEFT OUTER JOIN "Foos" AS "Foo" ON "User"."FooId" = "Foo"."id" LEFT OUTER JOIN "Bars" AS "Foo.Bar" ON "Foo"."BarId" = "Foo.Bar"."id";

and log to console

fooname:  undefined Foo.fooname:  B fooname:  undefined Foo.Bar.barname:  C

the results for fooname and barname are undefined

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

5reactions
bt404commented, May 25, 2018

After reading the source, I find the issue still exist. Can I open a PR to add a parameter to prevent adding a prefix of table name to the result column?

0reactions
Mohhd-Arifcommented, Oct 6, 2020

i am still getting the prefix as tablename. any solution yet?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to alias a table in Laravel Eloquent queries (or using ...
Laravel supports aliases on tables and columns with AS . Try $users = DB::table('really_long_table_name AS t') ->select('t.id AS uid') ->get();.
Read more >
G-3120: Always use table aliases when your SQL statement ...
G-3120: Always use table aliases when your SQL statement involves more than one source. Major. Maintainability. Reason. It is more human readable to...
Read more >
Raw Queries - Sequelize
By default the function will return two arguments - a results array, and an object containing metadata (such as amount of affected rows,...
Read more >
Rules Reference — SQLFluff 1.4.5 documentation
Querying all columns using * produces a query result where the number or ordering of columns changes if the upstream table's schema changes....
Read more >
Best practices for writing SQL queries - Metabase
Specify the columns you'd like to include in the results (though it's fine to use * when first exploring tables — just remember...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found