dolthub / dolt

Dolt – Git for Data
Apache License 2.0
17.41k stars 488 forks source link

ADD CONSTRAINT FOREIGN KEY causes syntax error in Dolt #8008

Closed arvidfm closed 1 month ago

arvidfm commented 1 month ago

The MySQL grammar for foreign keys is as follows:

[CONSTRAINT [symbol]] FOREIGN KEY
    [index_name] (col_name, ...)
    REFERENCES tbl_name (col_name,...)

but Dolt treats the first part as [CONSTRAINT symbol] FOREIGN KEY instead of [CONSTRAINT [symbol]] FOREIGN KEY, resulting in a syntax error if you include the CONSTRAINT keyword but not a name.

Trivial to work around by simply leaving out the CONSTRAINT keyword, but figured it's worth reporting still as it's technically a MySQL incompatibility that we've encountered in practice.

MWE:

DROP TABLE IF EXISTS constraint_test;

CREATE TABLE constraint_test (
    a INT PRIMARY KEY,
    b INT
);

ALTER TABLE constraint_test
    ADD CONSTRAINT FOREIGN KEY (`b`) REFERENCES constraint_test (`a`);

DROP TABLE constraint_test;
fulghum commented 1 month ago

Thanks for reporting this one! I've updated our parser to support using the CONSTRAINT keyword without supplying a constraint name when adding a foreign key constraint. I'll get that change propagated through our dependencies and then it'll be staged to go out with our next release.

fulghum commented 1 month ago

This fix made it out in Dolt 1.40.0. Let us know if you find anything else!