Skip to content

Commit 513985c

Browse files
Pierre-Luc Gagnéclaude
andcommitted
feat!: remove automatic PRIMARY KEY AUTO_INCREMENT on first column
The first column no longer implicitly receives PRIMARY KEY AUTO_INCREMENT. Define primary keys explicitly via table_constraints<T> or column attributes. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 3e7bba7 commit 513985c

5 files changed

Lines changed: 108 additions & 159 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ Versioning follows [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
88

99
## [Unreleased]
1010

11+
### Removed
12+
13+
- `table_inline_primary_key<T>` trait and automatic `PRIMARY KEY AUTO_INCREMENT` on the first column — define primary keys explicitly via `table_constraints<T>` or column attributes (`column_attr::auto_increment`)
14+
1115
---
1216

1317
## [4.4.1] – 2026-03-29

examples/tables_with_constraints.cpp

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
// tables_with_constraints.cpp — DSMySQL table constraints example.
22
//
33
// Demonstrates how to:
4-
// 1. Define table-level PRIMARY KEY, secondary indexes, and other constraints
4+
// 1. Define table-level constraints and secondary indexes
55
// 2. Customize CREATE TABLE output with table_constraints<T> trait
6-
// 3. Disable inline implicit primary key with table_inline_primary_key<T>
7-
// 4. Use table_constraint helpers: primary_key, key, unique_key, check, etc.
6+
// 3. Use table_constraint helpers: primary_key, key, unique_key, check, etc.
87
//
98
// Build with:
109
// cmake --preset release && cmake --build build -j$(nproc)
@@ -45,23 +44,14 @@ struct order_detail {
4544
};
4645

4746
// ===================================================================
48-
// Customize symbol table: define table-level PRIMARY KEY and secondary
49-
// indexes using the table_constraints<T> trait.
47+
// Customize symbol table: define secondary indexes using the
48+
// table_constraints<T> trait.
5049
// ===================================================================
5150

52-
template <>
53-
struct ds_mysql::table_inline_primary_key<symbol> {
54-
// Disable the default inline PRIMARY KEY AUTO_INCREMENT on first column
55-
static constexpr bool value = false;
56-
};
57-
5851
template <>
5952
struct ds_mysql::table_constraints<symbol> {
6053
static std::vector<std::string> get() {
6154
return {
62-
// Define table-level PRIMARY KEY(id)
63-
ds_mysql::table_constraint::primary_key<symbol::id>(),
64-
6555
// Define secondary index on exchange_id
6656
ds_mysql::table_constraint::key<symbol::exchange_id>(ds_mysql::index_id<"index_exchange_id">{}),
6757

@@ -75,18 +65,10 @@ struct ds_mysql::table_constraints<symbol> {
7565
// Customize order_detail table: define UNIQUE constraint and CHECK constraint
7666
// ===================================================================
7767

78-
template <>
79-
struct ds_mysql::table_inline_primary_key<order_detail> {
80-
static constexpr bool value = false;
81-
};
82-
8368
template <>
8469
struct ds_mysql::table_constraints<order_detail> {
8570
static std::vector<std::string> get() {
8671
return {
87-
// Define table-level PRIMARY KEY(id)
88-
ds_mysql::table_constraint::primary_key<order_detail::id>(),
89-
9072
// Define unique constraint on (order_id, line_number)
9173
ds_mysql::table_constraint::unique_key<order_detail::order_id, order_detail::line_number>(
9274
ds_mysql::index_id<"uq_order_line">{}),

lib/include/ds_mysql/schema_generator.hpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -469,9 +469,6 @@ template <typename T, std::size_t I>
469469
if constexpr (!is_optional_v<field_type>) {
470470
col += " NOT NULL";
471471
}
472-
if constexpr (I == 0) {
473-
col += " PRIMARY KEY AUTO_INCREMENT";
474-
}
475472
return col;
476473
}
477474

lib/include/ds_mysql/sql_ddl.hpp

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -75,21 +75,11 @@ enum class Encryption {
7575
// ===================================================================
7676
// CREATE TABLE constraint customization
7777
//
78-
// table_inline_primary_key<T>
79-
// - true (default): first column is emitted as `PRIMARY KEY AUTO_INCREMENT`
80-
// - false : first column is emitted as `AUTO_INCREMENT` only,
81-
// allowing table_constraints<T> to define table-level PK
82-
//
8378
// table_constraints<T>
8479
// - return additional table-level constraints/index definitions appended
8580
// inside CREATE TABLE (...)
8681
// ===================================================================
8782

88-
template <typename T>
89-
struct table_inline_primary_key {
90-
static constexpr bool value = true;
91-
};
92-
9383
template <typename T>
9484
struct table_constraints {
9585
[[nodiscard]] static std::vector<std::string> get() {
@@ -1034,12 +1024,6 @@ template <typename T, std::size_t I>
10341024
if constexpr (!is_field_nullable_v<field_type>) {
10351025
col += " NOT NULL";
10361026
}
1037-
if constexpr (I == 0) {
1038-
if constexpr (table_inline_primary_key<T>::value) {
1039-
col += " PRIMARY KEY AUTO_INCREMENT";
1040-
}
1041-
// When inline PK is disabled, typed column attributes should handle AUTO_INCREMENT
1042-
}
10431027
if constexpr (requires { field_type::ddl_auto_increment; } && field_type::ddl_auto_increment) {
10441028
col += " AUTO_INCREMENT";
10451029
}

0 commit comments

Comments
 (0)