PHP-SQL-Parser icon indicating copy to clipboard operation
PHP-SQL-Parser copied to clipboard

Identification of PRIMARY KEY depends on order

Open Gared opened this issue 3 years ago • 2 comments

Hey, first of all thanks for providing this extremely useful library. Unfortunately I have noticed this weird behaviour.

If i want to get the primary key of this sql statement:

CREATE TABLE IF NOT EXISTS `testabc` (
    `from` INT UNSIGNED NOT NULL,
    `to` INT UNSIGNED NOT NULL,
    INDEX `index_cccrange_from` (`from` ASC),
    INDEX `index_to` (`to` ASC),
    PRIMARY KEY (`from`, `to`)
);

This will return 3 indexes, but no primary key.

If I change the order to this statement it all works fine:

CREATE TABLE IF NOT EXISTS `testabc` (
    `from` INT UNSIGNED NOT NULL,
    `to` INT UNSIGNED NOT NULL,
    PRIMARY KEY (`from`, `to`),
    INDEX `index_cccrange_from` (`from` ASC),
    INDEX `index_to` (`to` ASC)
);

I don't know exactly why but the difference seems to be that when it is not working $prevCategory is "INDEX_COL_LIST" in https://github.com/greenlion/PHP-SQL-Parser/blob/e38d6f0f500d4d86bee7722e2e89262eeaab7e59/src/PHPSQLParser/processors/CreateDefinitionProcessor.php#L148. In the second (working) case $prevCategory is just an empty string.

Gared avatar Apr 26 '21 19:04 Gared

Probably just needs || $prevCategory == INDEX_COL_LIST - will take a look as soon as I can.

greenlion avatar Feb 01 '22 09:02 greenlion

I just encountered this bug myself. I created a quick tests to demonstrate it: https://gist.github.com/xsist10/9502ce318dc39ff25c24940f1378767c

xsist10 avatar Nov 30 '23 21:11 xsist10