Repository navigation
Closed
Labels
Description
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
1SQL 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)Reacted by Mike Estes, Geoffrey Thierry, Gangbaolede Li (Ganbold), Dave Welsh, Pierre Averous, Safi Nettah and Alexander
Activity
fix for #3350
pleerock commentedon Jan 3, 2019
Okay, its a bug.
Why it creates different queries? Because we added
limit 1tofindOneto 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.
pleerock commentedon Jan 3, 2019
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]Zclhlmgqzc commentedon Jan 8, 2019
@pleerock Bug [email protected]
error: { Error: Unknown column 'distinctAlias.SysUserEntity_user_id' in 'field list'
pleerock commentedon Jan 13, 2019
@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.
VinceOPS commentedon Jan 24, 2019
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
relationsin the FindOptions offindOne. It suddenly becameundefined, 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...
pleerock commentedon Jan 25, 2019
ah...
working "github issues > #3350 ER_DUP_FIELDNAME with simple find"