kysely-org / kysely

A type-safe typescript SQL query builder
https://kysely.dev
MIT License
10.71k stars 271 forks source link

introspection.getTables sorts column array alphabetically instead of by ordinal position #146

Closed jaylmiller closed 2 years ago

jaylmiller commented 2 years ago

Is this intentional? It is a bit counter intuitive to me and makes the introspection feature slightly less useful

would be happy to do a PR if you think this change would make sense.

koskimas commented 2 years ago

Why is it counter intuitive? And what is the ordinal position anyway in case of columns? And you know the order is intentional, since you've looked at the code. No need to be passive aggressive

wirekang commented 1 year ago

This issue should be reopen. Columns have a clear order regardless of dialect.

Mysql

information_schema.columns.ordinal_position

Postgres

inforamtion_schema.columns.ordinal_position or pg_catalog.pg_attribute.attnum

Sqlite

pragma_table_info(table).cid

Suggestion

Reverting part of f6588f8f could solve this problem. However, since there is no criterion for ordering the tables, sorting the tables by name seems necessary.

This line https://github.com/koskimas/kysely/blob/71507c4102ca1ae86fa18940c53310aa6e15ac1b/src/dialect/mysql/mysql-introspector.ts#L49

should be orderBy('ordinal_position')

This line https://github.com/koskimas/kysely/blob/71507c4102ca1ae86fa18940c53310aa6e15ac1b/src/dialect/postgres/postgres-introspector.ts#L80

should be orderBy('a.attnum')

This line https://github.com/koskimas/kysely/blob/71507c4102ca1ae86fa18940c53310aa6e15ac1b/src/dialect/sqlite/sqlite-introspector.ts#L85

should be orderBy('cid')