Skip to content

ER_DUP_FIELDNAME with simple find on 0.2.10 #3350

Closed

Description

@Slessi

Issue type:

[x] bug report

Database system/driver:

[x] mysql 5.7.24

TypeORM version:

[x] 0.2.10

Steps to reproduce or a small repository showing the problem:

Given the following two entities:

@Entity({ name: "payrolls" })
export class Payroll {
    @PrimaryGeneratedColumn()
    public id: number;
}

@Entity({ name: "payroll_payslips" })
export class PayrollPayslip {
    @PrimaryGeneratedColumn()
    public id: number;

    @Column({ name: "payroll_id" })
    public payrollId: number;

    @ManyToOne((type) => Payroll)
    @JoinColumn({ name: "payroll_id" })
    public payroll: Payroll;
}

And the following find:

dbConn.getRepository(PayrollPayslip).findOne(id, { relations: ["payroll"] });

I now receive ER_DUP_FIELDNAME after upgrading to 0.2.10

error: QueryFailedError: ER_DUP_FIELDNAME: Duplicate column name 'PayrollPayslip_payroll_id'
    at new QueryFailedError (<project_dir>/node_modules/typeorm/error/QueryFailedError.js:28:28)
    at Query.<anonymous> (<project_dir>/node_modules/typeorm/driver/mysql/MysqlQueryRunner.js:227:45)
    at Query.<anonymous> (<project_dir>/node_modules/mysql/lib/Connection.js:502:10)
    at Query._callback (<project_dir>/node_modules/mysql/lib/Connection.js:468:16)
    at Query.Sequence.end (<project_dir>/node_modules/mysql/lib/protocol/sequences/Sequence.js:83:24)
    at Query.ErrorPacket (<project_dir>/node_modules/mysql/lib/protocol/sequences/Query.js:90:8)
    at Protocol._parsePacket (<project_dir>/node_modules/mysql/lib/protocol/Protocol.js:278:23)
    at Parser.write (<project_dir>/node_modules/mysql/lib/protocol/Parser.js:76:12)
    at Protocol.write (<project_dir>/node_modules/mysql/lib/protocol/Protocol.js:38:16)
    at Socket.<anonymous> (<project_dir>/node_modules/mysql/lib/Connection.js:91:28)
    at Socket.<anonymous> (<project_dir>/node_modules/mysql/lib/Connection.js:502:10)
    at Socket.emit (events.js:182:13)
    at Socket.EventEmitter.emit (domain.js:441:20)
    at addChunk (_stream_readable.js:283:12)
    at readableAddChunk (_stream_readable.js:264:11)
    at Socket.Readable.push (_stream_readable.js:219:10)

SQL generated on 0.2.10 (this does not work):

SELECT
    DISTINCT `distinctAlias`.`PayrollPayslip_id` as "ids_PayrollPayslip_id"
FROM
    (
        SELECT
            -- other fields ommitted
            `PayrollPayslip`.`payroll_id` AS `PayrollPayslip_payroll_id`,
            `PayrollPayslip_payroll`.`id` AS `PayrollPayslip_payroll_id`
        FROM
            `payroll_payslips` `PayrollPayslip`
            LEFT JOIN `payrolls` `PayrollPayslip_payroll` ON `PayrollPayslip_payroll`.`id` = `PayrollPayslip`.`payroll_id`
        WHERE
            (`PayrollPayslip`.`id` = 62)
    ) `distinctAlias`
ORDER BY
    `PayrollPayslip_id` ASC
LIMIT
    1

SQL generated on 0.2.9 (this works):

SELECT
    -- other fields ommitted
    `PayrollPayslip`.`payroll_id` AS `PayrollPayslip_payroll_id`,
    `PayrollPayslip_payroll`.`id` AS `PayrollPayslip_payroll_id`
FROM
    `payroll_payslips` `PayrollPayslip`
    LEFT JOIN `payrolls` `PayrollPayslip_payroll` ON `PayrollPayslip_payroll`.`id` = `PayrollPayslip`.`payroll_id`
WHERE
    (`PayrollPayslip`.`id` = 62)

Activity

  1. added a commit that references this issue on Jan 3, 2019
  2. pleerock commented on Jan 3, 2019

    @pleerock
    Member

    Okay, its a bug.

    Why it creates different queries? Because we added limit 1 to findOne to prevent additional stuff loading. Currently, in query builder "special distinct-based query" is used in the case if either limit or offset is defined. In this case we don't need this special query, but this is too complex to resolve right now, and we already have a different relation load mechanizm in 0.3.0.

    But the actual bug is in query we had even in 0.2.9. It shouldn't select columns from two tables with same names. I have fixed that.

  3. pleerock commented on Jan 3, 2019

    @pleerock
    Member

    Published it under @rc, its a breaking bug, but its not super urgent since issue reproduces only when using custom join column names. npm i [email protected]

  4. Zclhlmgqzc commented on Jan 8, 2019

    @Zclhlmgqzc

    @pleerock Bug [email protected]

    findOne({
    select: ['password']
    })
    SELECT DISTINCT `distinctAlias`.`SysUserEntity_user_id` as "ids_SysUserEntity_user_id" FROM (SELECT `SysUserEntity`.`password` AS `SysUserEntity_password` FROM `sys_user` `SysUserEntity` LEFT JOIN `sys_dept` `SysUserEntity_dept` ON `SysUserEntity_dept`.`dept_id`=`SysUserEntity`.`dept_id`  LEFT JOIN `sys_user_role` `SysUserEntity_SysUserEntity_roles` ON `SysUserEntity_SysUserEntity_roles`.`user_id`=`SysUserEntity`.`user_id`
    LEFT JOIN `sys_role` `SysUserEntity_roles` ON `SysUserEntity_roles`.`role_id`=`SysUserEntity_SysUserEntity_roles`.`role_id` WHERE `SysUserEntity`.`username` = ?) `distinctAlias` ORDER BY `SysUserEntity_user_id` ASC LIMIT 1 

    error: { Error: Unknown column 'distinctAlias.SysUserEntity_user_id' in 'field list'

  5. pleerock commented on Jan 13, 2019

    @pleerock
    Member

    @Zclhlmgqzc as I answered in a separate issue you must select primary keys because ORM rely on them to make proper mapping of your relations.

  6. VinceOPS commented on Jan 24, 2019

    @VinceOPS
    Contributor

    Hi! Not sure I should open a new issue just for this, but it looks like this change introduced a bug in our application. I just reverted this single commit (and kept using the current master), and the bug disappeared.

    We have a deep nested relation that we fetch using relations in the FindOptions of findOne. It suddenly became undefined, with this commit. Getting back to a single underscore solved the problem: back to normal/expected behavior.

    I'd like to investigate and propose an additional fix. But I just wanted to let you know @pleerock as you might have an instant idea 🙄.

    Anyway, will keep you posted. thanks!

    EDIT - Ok, this is due to the max length of 63 characters that Postgre allows for aliases & column names...

  7. pleerock commented on Jan 25, 2019

    @pleerock
    Member

    Ok, this is due to the max length of 63 characters that Postgre allows for aliases & column names...

    ah...

  8. added a commit that references this issue on Mar 18, 2019
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

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions