Skip to content

Commit d9f74d9

Browse files
authored
Merge branch 'master' into master
2 parents 66c8857 + 5f4354d commit d9f74d9

2 files changed

Lines changed: 28 additions & 1 deletion

File tree

ormar/fields/foreign_key.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,8 @@ def ForeignKey( # type: ignore # noqa CFQ002
262262
sql_nullable = kwargs.pop("sql_nullable", None)
263263
sql_nullable = nullable if sql_nullable is None else sql_nullable
264264

265+
index = kwargs.pop("index", False)
266+
265267
validate_not_allowed_fields(kwargs)
266268
pk_only_model = None
267269
if to.__class__ == ForwardRef:
@@ -296,7 +298,7 @@ def ForeignKey( # type: ignore # noqa CFQ002
296298
related_name=related_name,
297299
virtual=virtual,
298300
primary_key=False,
299-
index=False,
301+
index=index,
300302
default=None,
301303
server_default=None,
302304
onupdate=onupdate,

tests/test_model_definition/test_model_definition.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,26 @@ class ExampleModel2(Model):
5454
test_string: str = ormar.String(max_length=250)
5555

5656

57+
class User(ormar.Model):
58+
ormar_config = base_ormar_config.copy(tablename="users")
59+
60+
id: int = ormar.Integer(primary_key=True)
61+
62+
63+
class Account(ormar.Model):
64+
ormar_config = base_ormar_config.copy(tablename="accounts")
65+
66+
id: int = ormar.Integer(primary_key=True)
67+
user: User = ormar.ForeignKey(User, index=False)
68+
69+
70+
class Purchase(ormar.Model):
71+
ormar_config = base_ormar_config.copy(tablename="purchases")
72+
73+
id: int = ormar.Integer(primary_key=True)
74+
user: User = ormar.ForeignKey(User, index=True)
75+
76+
5777
create_test_database = init_tests(base_ormar_config)
5878

5979

@@ -218,3 +238,8 @@ def test_json_conversion_in_model():
218238
test_string="test",
219239
test_bool=True,
220240
)
241+
242+
243+
def test_foreign_key_index():
244+
assert Account.ormar_config.table.columns.user.index is False
245+
assert Purchase.ormar_config.table.columns.user.index is True

0 commit comments

Comments
 (0)