New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CockroachDB inserting additional rowid
column when using .primary()
#5211
Comments
It looks like implementing a I have a basic PoC for PostgreSQL working locally, if you're ok with adding the option to Let me know if this would be something you would accept and I'll prepare a PR. |
@hairmare Isn't any column technically able to be turned into a primary key? Even though it's not common, I should be able to do something like |
@hairmare Solution looks good! |
@rijkvanzanten indeed, that should be possible, the underlying issue here is that CockroachDB will just create the |
Does/will every column type support the (Mostly asking out of selfishness; I'm working on a thing where the end-user chooses the type and whether or not it should be the primary key, so any deviation in the signatures there causes a bit more headache in the usage on our end 🙂 Beyond the bug presented above, it's currently nice-n-easy to do a const column = table[selectedType]();
if (userWantsThisToBeAPrimaryKey) {
column.primary();
} |
@rijkvanzanten I have no strong opinion on this; while technically it's a possibility, I'm yet to see non-number, non-string, non-uuid primary keys. |
afaict currently only it looks like adding support for calling this would result in needing to treat I'm not sure if it's currently supported but having mixed primary keys (as in |
Interesting! I thought they would be fairly common in the usage of junction tables, where you use the mixed primary key as the identifier of the junction row:
where the primary key of |
@rijkvanzanten yeah, junction tables. I usually tend to create them with their own separate primary key mainly for supporting multiple databases. I've probably been cargo-culting that pattern from an oci to mysql migration decades ago tho. |
To support composite keys, I thought to add to the table schema the addition of specifying the keys marked with the PRIMARY KEY (article_id, category_id) or we can do the same in PostgreSQL dialect: CONSTRAINT "primary" PRIMARY KEY (article_id ASC, category_id ASC) |
Generating this schema would be the ideal solution: CREATE TABLE articles_categories (
article_id UUID DEFAULT gen_random_uuid(),
category_id UUID,
PRIMARY KEY (article_id ASC, category_id ASC)
); For the The simplest thing left is to find the time to implement this :) |
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. |
Sweet! Thanks @hairmare 🙂 So just to confirm, The following accurate / intended? // Create auto-increment primary key (single query)
table.increments('x');
// Create auto-increment primary key (two queries)
table.increments('x', { primaryKey: false }).primary();
// Create UUID primary key (single query)
table.uuid('x', { primaryKey: true });
// Create UUID primary key (two queries)
table.uuid('x').primary();
// Create any other primary key (two queries)
table.integer('x').primary();
table.string('x').primary(); @kibertoad Is the accepted solution here not support single-query primary keys on anything that isn't auto-increment/uuid in PG/CRDB, or is the long-term goal to make |
@rijkvanzanten yes, i can/have to confirm that the behaviour won't be consistent wrt I will be doing some testing wrt this over the next few weeks & if it turns out to be an issue that affects a lot of users/sql-dialects, then i'll gladly look into how we can make my current understanding is that my change here in knex will enable finding a non-bc-breaking solution for directus/directus#13749 that should be acceptable whilst not constraining us wrt to a future fix that supports just using Once #5212 has been tagged, I'l look into finding a way forward (which might possibly involve adding more dialects that support |
I should also mention i do plan on looking into #5017, it does look like it contains a interesting approach that i missed |
I don't fully agree, but lets discuss that further on directus/directus#13749 rather than spamming this thread 🙂 |
Released in 2.2.0 |
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:
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: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 #4141 / #5017.
A temporary workaround is to use a
specificType
, for example:as that generates SQL that correctly creates the primary key in the first query in CRDB:
Reproduction
https://github.com/rijkvanzanten/knex-crdb-primary
The text was updated successfully, but these errors were encountered: