Add support for composite primary keys in Astro DB #910
Closed
ThomasGysemans
started this conversation in
Proposal
Replies: 1 comment
|
Astro DB has been deprecated |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Summary
In SQL it is possible to create a primary key that is a composite of several columns:
I'd like to be able to do the same in Astro DB.
Background & Motivation
It's quite common to reach such situation where a ManyToMany relationship leads to composite primary key in SQL. I wish it would be possible for Astro DB to make the creation of databases look more like plain SQL.
Goals
uniquerule across multiple columns.Example
I've encountered such situation where I have blog posts that all have 1 or more themes attached to them (in a table named "Article"). The themes are stored in another table named "Theme" and since it's a ManyToMany relationship my practice in SQL taught me that I should create a third table for linking the two that I named "ArticleThemes".
Here is what I'd write in plain SQL:
I'm a beginner in Astro and from what I have read from the documentation I'd implement it like this:
Unfortunately this isn't possible as
primaryKeycan only be applied to a single column. The code above produces an error that I think should not happen. I think it makes sense that ifprimaryKeyis set totrueto multiple columns then the SQL creation script should combine them.As of now (
v4.7.0of Astro andv0.10.6of AstroDB) I have no choice but to defineArticleThemeslike this:What's unfortunate is that I have no efficient way to make sure that the combination of
articleIdandthemeis always unique across the whole table, whereas I could if I was writing the SQL script directly.EDIT
Something I didn't know about when creating this discussion... it is actually possible to make sure the combination of
articleIdandthemeare always unique with an index:As explained here
However I maintain this discussion as I think a composite primary key would be a good thing to have in Astro DB.
All reactions