﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
34894	Query.change_aliases() has several significant bugs	Aqua Penguin	nobody	"When using the {{{Query.change_aliases(change_map)}}} method (on an extended class), some changes are not done and errors occur:
1. the insert order of {{{Query.alias_map}}} is modified, and it is used by the compiler to compile the FROM parts in the correct order, this can results in an inconsistant order of the JOIN clauses;

2. even if an entry of {{{Query.alias_map}}} is not in the {{{change_map}}}, its alias_data may still have to be relabeled if {{{alias_data.parent_alias}}} is in the {{{change_map}}}, but it is not done and the generated nested joins that are modified by {{{change_map}}} have errors in the generated ON clauses;

3. the cached property {{{Query.base_table}}} is not modified if it is in the {{{change_map}}}, it results in error when compiling the query, the old alias is searched in {{{Query.alias_refcount}}}, resulting in a KeyError.

*Solutions:*

1. change the for loop to iterate on the {{{Query.alias_map}}} instead of the {{{change_map}}}, and rebuild it in the same order with updates;

2. in addition to relabel the entries of {{{Query.alias_map}}} that are in the {{{change_map}}}, relabel for those whose the {{{alias_data.parent_alias}}} are in the {{{Query.alias_map}}};

3. change the cached property {{{Query.base_table}}} if it is in the {{{change_map}}}.


Attached files: patch with fixes and tests

Question: The bugs has been tested on 4.2 and main, I have to do a PR on all current versions or only on the main branch ?


"	Cleanup/optimization	closed	Database layer (models, ORM)	4.2	Normal	needsinfo	Query change_aliases	Aqua Penguin Simon Charette	Unreviewed	0	0	0	0	0	0
