xo / usql

Universal command-line interface for SQL databases
MIT License
8.82k stars 346 forks source link

`\d` returns incorrect foreign key constraint #392

Closed notramo closed 1 year ago

notramo commented 1 year ago

The \d <table_name> command returns incorrect foreign key constraint on MariaDB (not tested other database systems).

The output says the foreign key references the table it is in. E.g. there are companies and employees each employee has a company_id which is a foreign key index on companies(id), but usql incorrectly shows in \d employees that it's on employees(id).

nineinchnick commented 1 year ago

Thanks for reporting this. Can you include a full table schema (CREATE TABLE statements) and the output of \d companies and \d employees?

notramo commented 1 year ago

This is a minimal database, only the fields needed for reproducing this bug. I noticed that if the foreign key constraint is created on a database that doesn't have a column with the referenced column name (e.g. the foreign key constraint references the id column of the other table, but there is no id column in the current table), the result in the \d command output is (). In the first example, there is only the company_id field in the employees table, so the last line of the output shows () after the REFERENCES statement (last line on the screenshot).

image

After adding an id column to employees, it will show it as employees(id).

image

nineinchnick commented 1 year ago

Thanks for all the details. There was an obvious bug and we were missing tests for MySQL.