From 0db1670c01e05e8b4b233d3a9d62552df2d98c52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dr=2E=20Patrick=20Urbanke=20=28=E5=8A=89=E8=87=AA=E6=88=90?= =?UTF-8?q?=29?= Date: Thu, 1 Jan 2026 12:51:58 +0100 Subject: [PATCH] Documented support for multiple primary keys; resolves #107 (#115) --- docs/primary_key.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/docs/primary_key.md b/docs/primary_key.md index 9072c4b..b064438 100644 --- a/docs/primary_key.md +++ b/docs/primary_key.md @@ -27,6 +27,31 @@ CREATE TABLE IF NOT EXISTS "People"( ); ``` +### Multiple Primary Keys + +You can define multiple primary keys by using `sqlgen::PrimaryKey` on multiple fields. This will create a composite primary key. + +```cpp +struct Order { + sqlgen::PrimaryKey order_id; + sqlgen::PrimaryKey product_id; + int quantity; +}; +``` + +Now the generated SQL schema will look like this: + +```sql +CREATE TABLE IF NOT EXISTS "Order"( + "order_id" INTEGER NOT NULL, + "product_id" INTEGER NOT NULL, + "quantity" INTEGER NOT NULL, + PRIMARY KEY("order_id", "product_id") +); +``` + +Note that this is not supported in SQLite, as it does not support composite primary keys. + ### Auto-incrementing Primary Keys You can define an auto-incrementing primary key by providing `sqlgen::auto_incr` as the second template argument to `sqlgen::PrimaryKey`. The underlying type of an auto-incrementing primary key must be an integral type.