Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
Mar 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion ormar/fields/foreign_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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,
Expand Down
25 changes: 25 additions & 0 deletions tests/test_model_definition/test_model_definition.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down Expand Up @@ -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
Loading