8000 Only put `collation` on join columns, never `charset` by ruudk · Pull Request #9652 · doctrine/orm · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Only put collation on join columns, never charset #9652

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

Closed
wants to merge 1 commit into from

Conversation

ruudk
Copy link
Contributor
@ruudk ruudk commented Apr 11, 2022

The fix introduced in #9636 now breaks when working with integer relations.

Putting charset and collation on a text join column works fine, but when the
column is an integer, MySQL won't accept it:

CREATE TABLE table_name (
  // ..
  user_id INT CHARACTER SET utf8 NOT NULL COLLATE `utf8_unicode_ci`
  // ..
)

produces this error:

Query 1 ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CHARACTER SET utf8 NOT NULL COLLATE `utf8_unicode_ci`, type VARCHAR(255) NOT NUL' at line 1

Luckily the solution is simple. The charset is not needed as MySQL automatically derives that from
the collation when specified. It's also clever enough to ignore collation when the type is an integer.

Therefore the following works fine:

CREATE TABLE table_name (
  // ..
  user_id INT NOT NULL COLLATE `utf8_unicode_ci`,
  string_user_id VARCHAR(255) NOT NULL COLLATE `utf8_unicode_ci`,
  // ..
)

/cc @greg0ire

The fix introduced in doctrine#9636 now breaks when working with integer relations.

Putting `charset` and `collation` on a text join column works fine, but when the
column is an integer, MySQL won't accept it:

```sql
CREATE TABLE table_name (
  // ..
  user_id INT CHARACTER SET utf8 NOT NULL COLLATE `utf8_unicode_ci`
  // ..
)
```

produces this error:
```
Query 1 ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'CHARACTER SET utf8 NOT NULL COLLATE `utf8_unicode_ci`, type VARCHAR(255) NOT NUL' at line 1
```

Luckily the solution is simple. The charset is not needed as MySQL automatically derives that from
the `collation` when specified. It's also clever enough to ignore `collation` when the type is an integer.

Therefore the following works fine:
```sql
CREATE TABLE table_name (
  // ..
  user_id INT NOT NULL COLLATE `utf8_unicode_ci`,
  string_user_id VARCHAR(255) NOT NULL COLLATE `utf8_unicode_ci`,
  // ..
)
```
@ruudk
Copy link
Contributor Author
ruudk commented Apr 11, 2022

Unfortunately, this still breaks when working with binary types 🤯

contract_id BINARY(16) NOT NULL COLLATE `utf8_unicode_ci`
# Query 1 ERROR: COLLATION 'utf8_unicode_ci' is not valid for CHARACTER SET 'binary'

@ruudk
Copy link
Contributor Author
ruudk commented Apr 11, 2022

Is there a way to know which column types do support collation?

Maybe we can only set the collation when the type is char, varchar or text? See documentation: https://dev.mysql.com/doc/refman/8.0/en/charset-column.html

@greg0ire
Copy link
Member

@ruudk maybe we should revert this instead? It's already bad that the ORM has to know about charsets and collations, IMO it would be worse if it has to know about MySQL types. Maybe this should be somehow fixed in DBAL instead?

@ruudk
Copy link
Contributor Author
ruudk commented Apr 11, 2022

We can do that.

Maybe I can create a PR that adds support for charset and collation on JoinColumn? That would allow me to set up the correct collations for the joins without having to complicate the rest. I don't mind extra explicit configuration.

@greg0ire
Copy link
Member

PR for revert: #9654

@greg0ire
Copy link
Member

Maybe I can create a PR that adds support for charset and collation on JoinColumn?

That might make sense since it already exists on Column.

@greg0ire
Copy link
Member

Your future contribution might also fix #4286

@ruudk
Copy link
Contributor Author
ruudk commented Apr 12, 2022

Closing in favour of #9655

@ruudk ruudk closed this Apr 12, 2022
@ruudk ruudk deleted the fix-bug branch April 12, 2022 07:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
0