diff --git a/ormar/fields/base.py b/ormar/fields/base.py index 4e0fa3a0d..77b5a5934 100644 --- a/ormar/fields/base.py +++ b/ormar/fields/base.py @@ -286,6 +286,7 @@ def get_column(self, name: str) -> sqlalchemy.Column: self.column_type, *self.construct_constraints(), primary_key=self.primary_key, + autoincrement=self.autoincrement, nullable=self.sql_nullable, index=self.index, unique=self.unique, diff --git a/tests/test_model_definition/test_columns.py b/tests/test_model_definition/test_columns.py index 5a30f582b..35c06a23d 100644 --- a/tests/test_model_definition/test_columns.py +++ b/tests/test_model_definition/test_columns.py @@ -36,6 +36,13 @@ class Example(ormar.Model): size: MyEnum = ormar.Enum(enum_class=MyEnum, default=MyEnum.SMALL) +class ExampleNonAutoIncrement(ormar.Model): + ormar_config = base_ormar_config.copy(tablename="example_nonautoincrement") + + id: int = ormar.Integer(primary_key=True, autoincrement=False) + name: str = ormar.String(max_length=200, default="aaa") + + class EnumExample(ormar.Model): ormar_config = base_ormar_config.copy(tablename="enum_example") @@ -59,6 +66,18 @@ class WrongEnum(Enum): Example(size=WrongEnum.A) +@pytest.mark.asyncio +async def test_non_auto_increment_pk(): + async with base_ormar_config.database: + example = ExampleNonAutoIncrement(id=1) + await example.save() + + assert ( + ExampleNonAutoIncrement.ormar_config.table.columns["id"].autoincrement + is False + ) + + @pytest.mark.asyncio async def test_enum_bulk_operations(): async with base_ormar_config.database: