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

Add type validation for foreign key and one to one model consistency #1792

Merged
merged 14 commits into from
Dec 20, 2024

Conversation

Abdeldjalil-H
Copy link
Contributor

@Abdeldjalil-H Abdeldjalil-H commented Nov 29, 2024

Added validation to ensure consistency between the foreign key's expected model type and the actual model instance being assigned. This addresses an issue where mismatched model types could lead to silent failures or confusing database errors.

Description

Key changes:

  1. Added type checking in Model._set_kwargs() when setting foreign key values
  2. Raises a ValidationError with a clear error message when type mismatch occurs
  3. Uses type() is comparison to ensure exact type matching (not allowing inheritance)

Motivation and Context

This fixes issue #1791 where inconsistencies between ForeignKeyField model parameter and actual model types were not caught.

How Has This Been Tested?

Running tests.

Checklist:

  • My code follows the code style of this project.
  • My change requires a change to the documentation.
  • I have updated the documentation accordingly.
  • I have added the changelog accordingly.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.

@henadzit
Copy link
Contributor

@Abdeldjalil-H thank you for the PR!

A few comments:

  • We need to add tests demonstrating that the check works
  • Is the new check covers the case where the field is set to the model, e.g. instance.relation = relation? If not, this PR should add it.

@Abdeldjalil-H
Copy link
Contributor Author

@henadzit thank you for your comments. I'll work on that.

Btw do you think we should use isinstance or is it okay to use the exact type comparison?

@henadzit
Copy link
Contributor

Btw do you think we should use isinstance or is it okay to use the exact type comparison?

I think either will work since Tortoise only supports abstract class inheritance.

Copy link
Contributor

@henadzit henadzit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Abdeldjalil-H thank you for great work! I left a few more comments, please have a look!

CHANGELOG.rst Outdated Show resolved Hide resolved
tortoise/queryset.py Show resolved Hide resolved
tests/fields/test_fk.py Outdated Show resolved Hide resolved
tortoise/models.py Show resolved Hide resolved
Copy link
Contributor

@henadzit henadzit left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is an issue with CHANGELOG formatting and CI is failing. Apart from that the changes look good to me!

CHANGELOG.rst Outdated
Added
^^^^^
- Added type validation for foreign key fields to ensure type safety. Now raises ``ValidationError`` when assigning foreign key values with incorrect model types (#1792)
0.22.1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Formatting doesn't look correct here.

@Abdeldjalil-H
Copy link
Contributor Author

I don't know if the tests are failing because of the changes or other things. Is the database cleared after each test case? @henadzit

@henadzit
Copy link
Contributor

henadzit commented Dec 2, 2024

I don't know if the tests are failing because of the changes or other things. Is the database cleared after each test case? @henadzit

The test cases that are failing are based on test.TestCase which is using transactions to isolate tests. I have a hunch that your changes have an unexpected side effect that lead to these failures.

@henadzit
Copy link
Contributor

@Abdeldjalil-H can you please rebase your changes and correct the CHANGELOG? I improved test isolation in #1816, maybe, it will resolve issues in this PR.

@coveralls
Copy link

coveralls commented Dec 19, 2024

Pull Request Test Coverage Report for Build 12412674345

Details

  • 13 of 14 (92.86%) changed or added relevant lines in 2 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.003%) to 90.143%

Changes Missing Coverage Covered Lines Changed/Added Lines %
tortoise/models.py 12 13 92.31%
Totals Coverage Status
Change from base Build 12410555131: 0.003%
Covered Lines: 6381
Relevant Lines: 6963

💛 - Coveralls

@henadzit henadzit merged commit 3829267 into tortoise:develop Dec 20, 2024
8 checks passed
@henadzit henadzit mentioned this pull request Dec 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants