port #989 to new branch due to lack of perms to fix conflicts (#1276)

This commit is contained in:
collerek
2024-03-24 00:41:32 +01:00
committed by GitHub
parent 52d992d8c7
commit 5f4354d445
2 changed files with 28 additions and 1 deletions

View File

@ -262,6 +262,8 @@ def ForeignKey( # type: ignore # noqa CFQ002
sql_nullable = kwargs.pop("sql_nullable", None)
sql_nullable = nullable if sql_nullable is None else sql_nullable
index = kwargs.pop("index", False)
validate_not_allowed_fields(kwargs)
pk_only_model = None
if to.__class__ == ForwardRef:
@ -296,7 +298,7 @@ def ForeignKey( # type: ignore # noqa CFQ002
related_name=related_name,
virtual=virtual,
primary_key=False,
index=False,
index=index,
default=None,
server_default=None,
onupdate=onupdate,

View File

@ -54,6 +54,26 @@ class ExampleModel2(Model):
test_string: str = ormar.String(max_length=250)
class User(ormar.Model):
ormar_config = base_ormar_config.copy(tablename="users")
id: int = ormar.Integer(primary_key=True)
class Account(ormar.Model):
ormar_config = base_ormar_config.copy(tablename="accounts")
id: int = ormar.Integer(primary_key=True)
user: User = ormar.ForeignKey(User, index=False)
class Purchase(ormar.Model):
ormar_config = base_ormar_config.copy(tablename="purchases")
id: int = ormar.Integer(primary_key=True)
user: User = ormar.ForeignKey(User, index=True)
create_test_database = init_tests(base_ormar_config)
@ -218,3 +238,8 @@ def test_json_conversion_in_model():
test_string="test",
test_bool=True,
)
def test_foreign_key_index():
assert Account.ormar_config.table.columns.user.index is False
assert Purchase.ormar_config.table.columns.user.index is True