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.

CockroachDB inserting additional `rowid` column when using `.primary()`

See original GitHub issue

Environment

Knex version: 2.1.0 Database + version: CockroachDB v22.1 (v21 affected as well) OS: macOS 12.3.1

Bug

Behavior

When using CockroachDB, creating a primary key of type UUID:

await database.schema.createTable("example_a", (table) => {
	table.uuid("id").notNullable().primary();
});

will cause a secondary “rowid” column to be autogenerated. This is most likely due to the fact that knex generates two SQL queries to create this table+id combo:

db.schema
	.createTable("example_a", (table) => {
		table.uuid("id").notNullable().primary();
	})
	.toSQL();

// [
//   {
//     sql: 'create table "example_a" ("id" uuid not null)',
//     bindings: []
//   },
//   {
//     sql: 'alter table "example_a" add constraint "example_a_pkey" primary key ("id")',
//     bindings: []
//   }
// ]

The first SQL query executed in Cockroach will generate the default “rowid” column, as cockroach requires a primary key to exist in the table. This means the issue (and hopefully the fix) is very similar to https://github.com/knex/knex/issues/4141 / https://github.com/knex/knex/pull/5017.

A temporary workaround is to use a specificType, for example:

await database.schema.createTable("example_b", (table) => {
	table.specificType("id", "UUID PRIMARY KEY");
});

as that generates SQL that correctly creates the primary key in the first query in CRDB:

database.schema
	.createTable("example_b", (table) => {
		table.specificType("id", "UUID PRIMARY KEY");
	})
	.toSQL();

// [
//   {
//     sql: 'create table "example_b" ("id" UUID PRIMARY KEY)',
//     bindings: []
//   }
// ]

Reproduction

https://github.com/rijkvanzanten/knex-crdb-primary

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:17 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
hairmarecommented, Jun 7, 2022

I think #5212 is now ready. I didn’t add tests/support for composite keys since i think the change is already enough on it’s own and it’s somewhat offtopic wrt the itch i’m scratching anyways.

2reactions
hairmarecommented, Jun 6, 2022

It looks like implementing a primaryKey option as in table.uuid('id', {primaryKey: true}); would be much easier than adding support for table.uuid('id').primary().

I have a basic PoC for PostgreSQL working locally, if you’re ok with adding the option to uuid() I’ll look into implementing it across the board. The option would work like the primaryKey option on increments and bigincrements but it would default to false and only do something on PostgreSQL style databases.

Let me know if this would be something you would accept and I’ll prepare a PR.

Read more comments on GitHub >

github_iconTop Results From Across the Web

SERIAL | CockroachDB Docs
Create a table with a SERIAL column: CREATE TABLE increment (a SERIAL PRIMARY KEY); · Run four transactions for inserting rows: BEGIN; INSERT...
Read more >
Primary Key Constraint | CockroachDB Docs
A primary key constraint specifies columns that can be used to uniquely identify rows in a table.
Read more >
SQL FAQs | CockroachDB Docs
To bulk-insert data into an existing table, batch multiple rows in one multi-row INSERT statement and do not include the INSERT statements within...
Read more >
CREATE TABLE | CockroachDB Docs
In CockroachDB, every table requires a primary key. If one is not explicitly defined, a column called rowid of the type INT is...
Read more >
INSERT | CockroachDB Docs
The INSERT statement inserts one or more rows into a table. ... the column(s) with the unique constraint; it always uses the column(s)...
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