Skip to content

table name aliases for include can conflict #4315

Closed

Description

@User4martin

See example. Currently dots are allowed in table-/model-names.

var S = require('sequelize');

var sequelize = new S('seq_test', 'postgres', '', {
    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', {
    fooname: { type: S.STRING(50), allowNull: true},
    foopass: { type: S.STRING(50), allowNull: true},
});
var Bar = sequelize.define('Bar', {
    barname: { type: S.STRING(50), allowNull: true},
    barpass: { type: S.STRING(50), allowNull: true},
});
var FooBar = sequelize.define('Foo.Bar', {
    barname: { type: S.STRING(50), allowNull: true},
    barpass: { type: S.STRING(50), allowNull: true},
});

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

sequelize.sync()
.then(function (){
    User.findAll({
        include: [
            {   model: Foo,
                include: [
                    {   model: Bar, },
                ],
            },
            { model: FooBar },
        ],
    });
});

Sql produred

Executing (default): SELECT "User"."id", "User"."name", "User"."pass", "User"."createdAt", "User"."updatedAt", "User"."FooId", "Foo"."BarId", "Foo"."id" AS "Foo.id", "Foo"."fooname" AS "Foo.fooname", "Foo"."foopass" AS "Foo.foopass", "Foo"."createdAt" AS "Foo.createdAt", "Foo"."updatedAt" AS "Foo.updatedAt", "Foo"."BarId" AS "Foo.BarId", "Foo.Bar"."id" AS "Foo.Bar.id", "Foo.Bar"."barname" AS "Foo.Bar.barname", "Foo.Bar"."barpass" AS "Foo.Bar.barpass", "Foo.Bar"."createdAt" AS "Foo.Bar.createdAt", "Foo.Bar"."updatedAt" AS "Foo.Bar.updatedAt", "Foo.Bar"."id" AS "Foo.Bar.id", "Foo.Bar"."barname" AS "Foo.Bar.barname", "Foo.Bar"."barpass" AS "Foo.Bar.barpass", "Foo.Bar"."createdAt" AS "Foo.Bar.createdAt", "Foo.Bar"."updatedAt" AS "Foo.Bar.updatedAt" 
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" 
LEFT OUTER JOIN "Foo.Bars" AS "Foo.Bar" ON "User"."Foo.BarId" = "Foo.Bar"."id";

Error

Unhandled rejection SequelizeDatabaseError: table name "Foo.Bar" specified more than once

Two ideas how to generate truly unique aliases.

Prefik (or append) the length of each name to the name (separated by a dot or _ to avoid conflict, if the name starts/ends with a number)

"3.Foo" 
"3.Foo.3.Bar"
"7.Foo.Bar"

or

 "Foo.3"
"Foo.3.Bar.3"
"Foo.Bar.7"

Those aliases become pretty big.
With dots, inside the alias and between alias and column, to me they rather hinder than help human readability.
So create short aliases, take up to 3 letters, and keep a map. If the same 3 letters happen again append a number. Then join with _ .

Activity

  1. added
    type: bug
    on Aug 17, 2015
  2. mickhansen commented on Aug 17, 2015

    @mickhansen
    Contributor

    Had not considered this issue, you faced this in actual code?

  3. User4martin commented on Aug 17, 2015

    @User4martin
    ContributorAuthor

    No not in real code.

    It realized it while discussing the "mentioning" issue above,

  4. stale commented on Jun 29, 2017

    @stale

    This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment 🙂

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

    staletype: bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions