Inconsistent constraint names in PostgreSQL
See original GitHub issueEnvironment
Knex version: 0.95.1 Database + version: PostgreSQL 13 OS: n/a
Bug
Run the code:
knex.schema.createTable('TestTable', (table) => {
table.integer('otherId').primary().notNull().references('Other.id');
}).debug();
It generates the following queries:
[
{
sql: 'create table "TestTable" ("otherId" integer not null)',
bindings: []
},
{
sql: 'alter table "TestTable" add constraint "TestTable_pkey" primary key ("otherId")',
bindings: []
},
{
sql: 'alter table "TestTable" add constraint "testtable_otherid_foreign" foreign key ("otherId") references "Other" ("id")',
bindings: []
}
]
Notice how one constraint name correctly preserves the PascalCase (TestTable_pkey
) while the other one ignores it (testtable_otherid_foreign
). Knex should be consistent and either always keep the original formatting or always convert it to lower case. I’d definitely prefer the former.
Issue Analytics
- State:
- Created 3 years ago
- Comments:5
Top Results From Across the Web
Is there inconsistency in PostgreSQL syntax of adding ...
To add a constraint, the table constraint syntax is used. For example: ALTER TABLE products ADD CHECK (name <> ''); ...
Read more >Thread: Error message inconsistency - Postgres Professional
As noted by a PostgreSQL user to me, error messages for NOT NULL constraints are inconsistent - they do not mention the relation...
Read more >Documentation: 15: 5.4. Constraints - PostgreSQL
Unique constraints ensure that the data contained in a column, or a group of columns, is unique among all the rows in the...
Read more >Constraint name visibility on MySQL and PostgreSQL
Constraint name visibility on MySQL and PostgreSQL. Can one have two tables with constraints of the same name?
Read more >[Solved]-Selecting on names has inconsistent behavior-postgresql
Values of type character are physically padded with spaces to the specified width n, and are stored and displayed that way. However, trailing...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@sebastian-nowak I’m mostly concerned that at this point changing logic would be something of a breaking change, because it is possible that someone wrote constraint-manipulating SQL (e. g. for dropping some of the constraints by name) taking the knex behaviour into consideration, and changing observable behaviour outside of semver major is not a good thing to do. And considering that original logic was there at least from 2016, and it didn’t come up until now, I think it’s not that big of an issue for the majority of the users.
That’s a good point, it could affect existing migrations. One possible solution is to make the new behavior opt-in in knex config, and it would become enabled by default only when there’s a major release.
I think the majority of users prefer snake case for columns names, so they were not affected. Bigger projects would usually set key and constraint names manually to avoid surprises like this, so they wouldn’t notice it too.