File tree Expand file tree Collapse file tree
tests/test_model_definition Expand file tree Collapse file tree Original file line number Diff line number Diff 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 ,
Original file line number Diff line number Diff 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+
5777create_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
You can’t perform that action at this time.
0 commit comments