[MDEV-11117] CHECK constraint fails on intermediate step of ALTER Created: 2016-10-23 Updated: 2020-04-27 Resolved: 2017-04-18 |
|
| Status: | Closed |
| Project: | MariaDB Server |
| Component/s: | Data Definition - Alter Table |
| Affects Version/s: | 10.2 |
| Fix Version/s: | 10.2.6 |
| Type: | Bug | Priority: | Major |
| Reporter: | Elena Stepanova | Assignee: | Jacob Mathew (Inactive) |
| Resolution: | Fixed | Votes: | 0 |
| Labels: | 10.2-ga | ||
| Issue Links: |
|
||||||||
| Sprint: | 10.2.6-3 | ||||||||
| Description |
|
If there were no CHECK, a column would have been given the value 1 which actually satisfies the condition.
But apparently the column gets the intermediate value 0 which is checked before the auto_increment is applied. |
| Comments |
| Comment by Jacob Mathew (Inactive) [ 2017-02-16 ] | |||||||||||||||||||
|
At the point when the check constraint is evaluated during the ALTER TABLE, the information that the column is NULLable and that the row's column value is NULL has been removed, but the column's value has not yet been updated with the auto-increment value, so the check constraint appears to be violated. | |||||||||||||||||||
| Comment by Jacob Mathew (Inactive) [ 2017-02-17 ] | |||||||||||||||||||
|
ALTER TABLE is unnecessary to reproduce the problem regarding check constraints on an auto-increment column:
Without the check constraint, the auto-increment column gets a value of 1. Here is what happens with the check constraint:
This occurs because auto-increment columns are given their incremented value as part of the storage engine process of writing a row, whereas check constraints are evaluated on the row prior to writing the row, at a time when the auto-increment column has been cleared and therefore has a value of zero. | |||||||||||||||||||
| Comment by Sergei Golubchik [ 2017-02-19 ] | |||||||||||||||||||
|
Let's disallow auto-increment columns in CHECK constraints. They are similarly disallowed in virtual columns and should be disallowed in DEFAULT expressions too (please check that). We'd need to refactor auto-increment implementation to make all this working as expected. It cannot be done in 10.2. | |||||||||||||||||||
| Comment by Jacob Mathew (Inactive) [ 2017-04-04 ] | |||||||||||||||||||
|
I have fixed the bug by disallowing auto-increment columns in CHECK constraints, DEFAULT expressions and VIRTUAL COLUMN expressions in commit https://github.com/MariaDB/server/commit/b7aa15458db95149af4f473a90009844a16e9db8. | |||||||||||||||||||
| Comment by Jacob Mathew (Inactive) [ 2017-04-04 ] | |||||||||||||||||||
|
Hi Sergei, I've fixed the bug by disallowing auto-increment columns in CHECK constraints, DEFAULT value expressions and VIRTUAL COLUMN expressions, in commit https://github.com/MariaDB/server/commit/b7aa15458db95149af4f473a90009844a16e9db8. Please review. Thanks, | |||||||||||||||||||
| Comment by Jacob Mathew (Inactive) [ 2017-04-18 ] | |||||||||||||||||||
|
I have merged the final version of the fix, committed in https://github.com/MariaDB/server/commit/38af34bb2143f2a7ce82d2e241d8995f419a7f29, into the 10.2 branch. | |||||||||||||||||||
| Comment by Jacob Mathew (Inactive) [ 2017-04-18 ] | |||||||||||||||||||
|
I have merged the final version of the fix, committed in https://github.com/MariaDB/server/commit/38af34bb2143f2a7ce82d2e241d8995f419a7f29, into the 10.2 branch. | |||||||||||||||||||
| Comment by Oleksandr Diedyk [ 2020-04-27 ] | |||||||||||||||||||
|
By making this fix, you've denied using columns in kind of 'recursive' approach.
So check constraint here is not allowed anymore. | |||||||||||||||||||
| Comment by Sergei Golubchik [ 2020-04-27 ] | |||||||||||||||||||
|
because of the way auto-increment columns are implemented, neither check constraints, nor virtual columns, not default expressions work with them, unfortunately. as a workaround you might try to use sequences, perhaps they'll help. |