question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Inconsistent constraint names in PostgreSQL

See original GitHub issue

Environment

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:open
  • Created 3 years ago
  • Comments:5

github_iconTop GitHub Comments

1reaction
kibertoadcommented, Mar 6, 2021

@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.

0reactions
sebastian-nowakcommented, Mar 11, 2021

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.

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.

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found