Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for AR case insensitive relation #17595

Closed
wants to merge 5 commits into from

Conversation

onmotion
Copy link
Contributor

@onmotion onmotion commented Oct 5, 2019

Q A
Is bugfix?
New feature?
Breaks BC?
Tests pass? ✔️
Fixed issues #9077

Related to yiisoft/active-record#22

@samdark samdark added this to the 2.0.29 milestone Oct 5, 2019
* For example, mysql can join records by strings with different cases (Key = key), but AR doesn't
* So if this param is true, AR will populate these records
*/
public $caseInsensitiveKeys = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think "caseSensitiveKeys" is better than "caseInsensitiveKeys" to avoid double negative

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After thinking about it, it seems that the whole issue could be considered a bug and the option isn't needed. Case insensitive comparison should be always used.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@samdark AFAIK it depends on DBMS. If comparison is case sensitive on DB layer, then it should be case sensitive also in AR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really. In case of case sensitive comparison on DB layer JOIN would not return anything so there will be no population to do.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure about that? I thought that related models are always populated by separate query, so there is no JOIN to filter results. With eager loading you can populate records for both key1 and Key1 in one query - they will mix each other if you ignore case.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@samdark MySQL tests failed. These tests are run only on MySQL.

@onmotion This is result of eager loading optimization. Such case is handled by 2 queries:

  1. Find all products (select * from product)
  2. Find all related attributes for products selected in first query (select * from product_attribute where product_sku in ('ARTi01', 'ARTI01')).

If you ignore case on processing related records, attributes for 'ARTi01' and 'ARTI01' will be mixed and attributes from ARTi01 will appear in ARTI01.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@onmotion what do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mb we can use caseSensitiveKeys = true option by default? Any proposals by @rob006 ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. That might be a good thing to do.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest, I don't really like this solution. Whether this key should be case-sensitive depends on column collation (which could be overwritten by query). ActiveRecord does not look like a good place for caseInsensitiveKeys setting. After this change AR assumes which DBMS is used and with what collation - neither of it should be AR concern.

Copy link
Member

@samdark samdark left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need a changelog line for it.

framework/db/ActiveRelationTrait.php Outdated Show resolved Hide resolved
framework/db/ActiveRelationTrait.php Outdated Show resolved Hide resolved
@samdark samdark modified the milestones: 2.0.29, 2.0.30 Oct 22, 2019
@samdark samdark added the type:bug Bug label Oct 22, 2019
* For example, mysql can join records by strings with different cases (Key = key), but AR doesn't
* So if this param is true, AR will populate these records
*/
public $caseInsensitiveKeys = false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After thinking about it, it seems that the whole issue could be considered a bug and the option isn't needed. Case insensitive comparison should be always used.

@samdark samdark added the status:under development Someone is working on a pull request. label Oct 22, 2019
@samdark samdark added status:code review The pull request needs review. and removed status:under development Someone is working on a pull request. labels Oct 22, 2019
@samdark samdark modified the milestones: 2.0.30, 2.0.31 Nov 19, 2019
@samdark
Copy link
Member

samdark commented Dec 4, 2019

Thought more about it. While it may fix the issue in your concrete case, it is potentially adding more trouble than it's solving. Thanks for trying to solve it but I don't think we'll merge the pull request. Sorry.

@samdark samdark closed this Dec 4, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status:code review The pull request needs review. type:bug Bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants