Skip to content

Commit f57f858

Browse files
smuethingcollerek
andauthored
Forward autoincrement property from field to column (#674)
* Forward autoincrement property from field to column The autoincrement property of ormar fields did not get propagated to the SQLAlchemy column definition. As a result, the autoincrement property of the SQLAlchemy column defaulted to `auto`, making it impossible to create DDL that did not auto-increment the database column. * Add test for non-autoincrement primary key column Verifies that autoincrement=False on an Integer primary key field is forwarded to the underlying SQLAlchemy column. * Use ruff format instead of black in Makefile Aligns the fmt target with master's tooling so contributors do not need black installed locally. --------- Co-authored-by: collerek <collerek@gmail.com>
1 parent accc115 commit f57f858

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

ormar/fields/base.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ def get_column(self, name: str) -> sqlalchemy.Column:
286286
self.column_type,
287287
*self.construct_constraints(),
288288
primary_key=self.primary_key,
289+
autoincrement=self.autoincrement,
289290
nullable=self.sql_nullable,
290291
index=self.index,
291292
unique=self.unique,

tests/test_model_definition/test_columns.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,13 @@ class Example(ormar.Model):
3636
size: MyEnum = ormar.Enum(enum_class=MyEnum, default=MyEnum.SMALL)
3737

3838

39+
class ExampleNonAutoIncrement(ormar.Model):
40+
ormar_config = base_ormar_config.copy(tablename="example_nonautoincrement")
41+
42+
id: int = ormar.Integer(primary_key=True, autoincrement=False)
43+
name: str = ormar.String(max_length=200, default="aaa")
44+
45+
3946
class EnumExample(ormar.Model):
4047
ormar_config = base_ormar_config.copy(tablename="enum_example")
4148

@@ -59,6 +66,18 @@ class WrongEnum(Enum):
5966
Example(size=WrongEnum.A)
6067

6168

69+
@pytest.mark.asyncio
70+
async def test_non_auto_increment_pk():
71+
async with base_ormar_config.database:
72+
example = ExampleNonAutoIncrement(id=1)
73+
await example.save()
74+
75+
assert (
76+
ExampleNonAutoIncrement.ormar_config.table.columns["id"].autoincrement
77+
is False
78+
)
79+
80+
6281
@pytest.mark.asyncio
6382
async def test_enum_bulk_operations():
6483
async with base_ormar_config.database:

0 commit comments

Comments
 (0)