norm icon indicating copy to clipboard operation
norm copied to clipboard

Using a different name than "id" for the primary key

Open saemideluxe opened this issue 2 years ago • 8 comments

saemideluxe avatar May 18 '22 08:05 saemideluxe

Okay, so this actually going to be a real issue I would like to open, as I did not find something about it in the docs or other issues: Is it possible to read from a database whose tables do not have a column "id" as the primary key but something else? I am mainly concerned about accessing existing sqlite databases, and this might be an issue for me. (Also, as a side note: I might start to work on a transpiler which parses sqlite schemas and produces norm types as output, for the same reason)

saemideluxe avatar May 18 '22 12:05 saemideluxe

I've looked into this issue and found the blocker that I need to resolve in order to proceed with the actual solution.

Currently, the SQL that is generated for Postgres doesn't allow any customization in primary key name or type. It's hardcoded as colShmParts.add "BIGSERIAL PRIMARY KEY". I need to switch to the more flexible Postgres syntax for primary key declaration.

I've been really busy with other stuff lately but I'm determined to find the time to move Norm forward.

moigagoo avatar Jun 04 '22 07:06 moigagoo

Great, thanks for the response :-)

saemideluxe avatar Jun 04 '22 08:06 saemideluxe

I was just considering using norm to clean up my code that has way too much raw SQL, but this seems like a big issue. I'd like to create sqlite tables that have a custom name for the primary key, with value that I provide, not generated automatically.

sgmihai avatar Jul 31 '22 15:07 sgmihai

I did approach this problem but it turned out to be a lot harder than I had thought initially. It's very convenient to have "id" as a reserved special column name for query generation, so there are many places in the code where this is used.

moigagoo avatar Aug 23 '22 08:08 moigagoo

I see. Thanks for looking at it anyway!

saemideluxe avatar Aug 24 '22 02:08 saemideluxe

I was similarly hoping there was a way to use something other than "id". We prefix all our id columns with the table name, such as "customerId" where "customer" is the name of the table. Would that be possible to handle as an enhancement?

gwque avatar Dec 12 '23 18:12 gwque

@gwque unfortunately id is a reserved name and many places in the code depend on it.

You can have your customers and stuff along with id if that's a viable option.

moigagoo avatar Dec 19 '23 16:12 moigagoo