Skip to content

Conversation

@s-rakowski
Copy link
Contributor

Currently, when trying to fetch a related object that has an integer primary key that equals to 0, the foreign key is set to None instead and the related object is not fetched.

Description

Although it's uncommon, it's possible to have a row with integer primary key equal to 0 or even negative.
An application I am working at has a table with such a row, and another table relates to it, prefetch_related and fetch_related doesn't work correctly. Instead of fetching the related object, the foreign key field is set to None.
This pull request tries to fix that.

Motivation and Context

This pull request attempts to fix the issue described above.

How Has This Been Tested?

I've added a test in test_relations.py called test_fetch_fk_zero. It creates two Tournament objects with hard-coded IDs of 0 and 1, and then creates an Event for both of them. The test fails before my change, because the Tournament with ID=0 is not fetched when trying to use both fetch_related and prefetch_related on an Event.
The change doesn't seem to affect other tests, but because this is my first attempt to contribute to this project, it would be good for someone else to verify.

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.

@s-rakowski
Copy link
Contributor Author

Looks like my test fails with MySQL. I'll check what happens later.

@s-rakowski
Copy link
Contributor Author

It turns out there has already been a similar issue discussed in #1275, sorry.
I moved my tests to already existing test case test_0_value_fk.
This test case skips MySQL, however it should be possible to make it pass with MySQL. This is because by default MySQL treats ID=0 as a request to get next number from the sequence. The following queries should allow the test to pass, at least with the tournaments table:

SET sql_mode = 'NO_AUTO_VALUE_ON_ZERO';
ALTER TABLE tournament AUTO_INCREMENT = 0;

However, I'm not sure if there's a good way to conditionally add these to the test case.

@long2ice long2ice merged commit f97f2c5 into tortoise:develop Apr 18, 2023
@long2ice
Copy link
Member

Thanks!

@s-rakowski s-rakowski deleted the support_fk_id_0 branch April 18, 2023 12:20
waketzheng pushed a commit to waketzheng/tortoise-orm that referenced this pull request Jun 21, 2023
* Fix fetching related object with ID = 0

* Move new fk 0 tests to existing test_0_value_fk test case

---------

Co-authored-by: Szymon Rakowski <szymon.rakowski@ac.com.pl>
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.

2 participants