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

Fail to alter integer column with a foreign key #47702

Closed
YangKeao opened this issue Oct 17, 2023 · 2 comments · Fixed by #49838
Closed

Fail to alter integer column with a foreign key #47702

YangKeao opened this issue Oct 17, 2023 · 2 comments · Fixed by #49838
Assignees
Labels
component/ddl This issue is related to DDL of TiDB. severity/moderate type/bug The issue is confirmed as a bug.

Comments

@YangKeao
Copy link
Member

Bug Report

1. Minimal reproduce step (Required)

mysql> create table parent(id bigint(10) key);
Query OK, 0 rows affected (0.05 sec)

mysql> create table child(pid bigint(20), foreign key (pid) references parent(id));
Query OK, 0 rows affected (0.03 sec)

mysql> alter table child modify column pid bigint(1);
ERROR 1832 (HY000): Cannot change column 'pid': used in a foreign key constraint 'fk_1'

2. What did you expect to see? (Required)

mysql> create table parent(id bigint(10) key);
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> create table child(pid bigint(20), foreign key (pid) references parent(id));
Query OK, 0 rows affected, 1 warning (0.02 sec)

mysql> alter table child modify column pid bigint(1);
Query OK, 0 rows affected, 1 warning (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 1

3. What did you see instead (Required)

TiDB failed to alter table.

4. What is your TiDB version? (Required)

master 2482a6e

@YangKeao YangKeao added the type/bug The issue is confirmed as a bug. label Oct 17, 2023
@YangKeao
Copy link
Member Author

YangKeao commented Oct 17, 2023

The following lines of checkModifyColumnWithForeignKeyConstraint gives this error:

				if newCol.GetFlen() < referCol.GetFlen() || newCol.GetFlen() < originalCol.GetFlen() ||
					(newCol.GetType() == mysql.TypeNewDecimal && (newCol.GetFlen() != originalCol.GetFlen() || newCol.GetDecimal() != originalCol.GetDecimal())) {
					return dbterror.ErrForeignKeyColumnCannotChange.GenWithStackByArgs(originalCol.Name, fkInfo.Name)
				}

Actually this condition can be relaxed, because the length of integer does nothing on the data 🤔 . See the needChangeColumnData:

		case mysql.TypeTiny, mysql.TypeShort, mysql.TypeInt24, mysql.TypeLong, mysql.TypeLonglong:
			return toUnsigned != originUnsigned

@YangKeao YangKeao added component/ddl This issue is related to DDL of TiDB. severity/moderate labels Oct 17, 2023
@yoshikipom
Copy link
Contributor

/assign

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component/ddl This issue is related to DDL of TiDB. severity/moderate type/bug The issue is confirmed as a bug.
Projects
None yet
2 participants