Skip to content

Commit 8c9ffcb

Browse files
booqoffskycollerek
andauthored
Pass ForeignKey kwargs to BaseField (#1239)
* fix: pass kwargs to BaseField * add also kwargs to many 2 many field --------- Co-authored-by: collerek <collerek@gmail.com>
1 parent 3462a00 commit 8c9ffcb

3 files changed

Lines changed: 37 additions & 0 deletions

File tree

ormar/fields/foreign_key.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,7 @@ def ForeignKey( # type: ignore # noqa CFQ002
313313
related_orders_by=related_orders_by,
314314
skip_reverse=skip_reverse,
315315
skip_field=skip_field,
316+
**kwargs,
316317
)
317318

318319
Field = type("ForeignKey", (ForeignKeyField, BaseField), {})

ormar/fields/many_to_many.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ def ManyToMany( # type: ignore
185185
through_reverse_foreign_key_name=through_reverse_foreign_key_name,
186186
through_relation_nullable=through_relation_nullable,
187187
through_reverse_relation_nullable=through_reverse_relation_nullable,
188+
**kwargs,
188189
)
189190

190191
Field = type("ManyToMany", (ManyToManyField, BaseField), {})

tests/test_model_definition/test_setting_comments_in_db.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,27 @@
88
base_ormar_config = create_config()
99

1010

11+
class Author(Model):
12+
ormar_config = base_ormar_config.copy(tablename="comment_authors")
13+
14+
id: int = ormar.Integer(primary_key=True)
15+
name: str = ormar.String(max_length=80)
16+
17+
18+
class Tag(Model):
19+
ormar_config = base_ormar_config.copy(tablename="comment_tags")
20+
21+
id: int = ormar.Integer(primary_key=True)
22+
name: str = ormar.String(max_length=40)
23+
24+
1125
class Comment(Model):
1226
ormar_config = base_ormar_config.copy(tablename="comments")
1327

1428
test: int = ormar.Integer(primary_key=True, comment="primary key of comments")
1529
test_string: str = ormar.String(max_length=250, comment="test that it works")
30+
author: Author = ormar.ForeignKey(Author, comment="author of the comment")
31+
tags = ormar.ManyToMany(Tag, comment="tags attached to the comment")
1632

1733

1834
create_test_database = init_tests(base_ormar_config)
@@ -23,3 +39,22 @@ async def test_comments_are_set_in_db():
2339
columns = Comment.ormar_config.table.c
2440
for c in columns:
2541
assert c.comment == Comment.ormar_config.model_fields[c.name].comment
42+
43+
44+
def test_foreign_key_comment_reaches_field_and_column():
45+
assert (
46+
Comment.ormar_config.model_fields["author"].comment == "author of the comment"
47+
)
48+
assert Comment.ormar_config.table.c.author.comment == "author of the comment"
49+
50+
51+
def test_many_to_many_comment_stored_on_field():
52+
# M2M has no column on the owner model's table and does not propagate the
53+
# comment to the through model's columns, so the field attribute is the
54+
# only observable side effect.
55+
assert (
56+
Comment.ormar_config.model_fields["tags"].comment
57+
== "tags attached to the comment"
58+
)
59+
through = Comment.ormar_config.model_fields["tags"].through
60+
assert all(c.comment is None for c in through.ormar_config.table.c)

0 commit comments

Comments
 (0)