Skip to content

Commit f916387

Browse files
jtmonroepre-commit-ci[bot]agronholm
authored
Autoincrement multiple primary keys (#474)
* feat(added autoincrement to primary key columns) * test(added test to show autoincrement behavior works) * update(added new changelog) * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update CHANGES.rst --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Alex Grönholm <alex.gronholm@nextday.fi>
1 parent 66b2e04 commit f916387

3 files changed

Lines changed: 36 additions & 0 deletions

File tree

CHANGES.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
Version history
22
===============
33

4+
**UNRELEASED**
5+
6+
- Added autoincrement to primary key columns to prevent missing field errors.
7+
(`#473 <https://github.com/agronholm/sqlacodegen/issues/473>`_; PR by @jtmonroe)
8+
49
**4.0.3**
510

611
- Improved rendering of ``Identity`` server defaults by explicitly rendering

src/sqlacodegen/generators.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,9 @@ def render_column(
471471
)
472472
or column.primary_key
473473
)
474+
is_autoincrement = (
475+
isinstance(column.autoincrement, bool) and column.autoincrement
476+
)
474477
has_index = any(
475478
set(i.columns) == {column} and uses_default_name(i)
476479
for i in column.table.indexes
@@ -494,6 +497,8 @@ def render_column(
494497
kwargs["key"] = column.key
495498
if is_primary:
496499
kwargs["primary_key"] = True
500+
if is_autoincrement and is_primary:
501+
kwargs["autoincrement"] = True
497502
if not column.nullable and not column.primary_key:
498503
kwargs["nullable"] = False
499504
if column.nullable and is_part_of_composite_pk:

tests/test_generator_declarative.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,6 +1497,32 @@ class SimpleItems(Base):
14971497
)
14981498

14991499

1500+
def test_composite_autoincrement_pk(generator: CodeGenerator) -> None:
1501+
Table(
1502+
"simple_autoincrement_items",
1503+
generator.metadata,
1504+
Column("id1", INTEGER, primary_key=True, autoincrement=True),
1505+
Column("id2", INTEGER, primary_key=True),
1506+
)
1507+
validate_code(
1508+
generator.generate(),
1509+
"""\
1510+
from sqlalchemy import Integer
1511+
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
1512+
1513+
class Base(DeclarativeBase):
1514+
pass
1515+
1516+
1517+
class SimpleAutoincrementItems(Base):
1518+
__tablename__ = 'simple_autoincrement_items'
1519+
1520+
id1: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
1521+
id2: Mapped[int] = mapped_column(Integer, primary_key=True)
1522+
""",
1523+
)
1524+
1525+
15001526
def test_joined_inheritance(generator: CodeGenerator) -> None:
15011527
Table(
15021528
"simple_sub_items",

0 commit comments

Comments
 (0)