Skip to content

Comments

1107 Fix prefetch when a ForeignKey has db_column_name set#1111

Open
dantownsend wants to merge 12 commits intomasterfrom
1107-fix-prefetch-when-fk-has-db-column-name
Open

1107 Fix prefetch when a ForeignKey has db_column_name set#1111
dantownsend wants to merge 12 commits intomasterfrom
1107-fix-prefetch-when-fk-has-db-column-name

Conversation

@dantownsend
Copy link
Member

Resolves #1107

@dantownsend dantownsend added the bug Something isn't working label Oct 18, 2024
@VladislavYar
Copy link

@dantownsend Hi. Will it ever reach release? Without this fix, it's hard to switch from Django ORM.

@dantownsend
Copy link
Member Author

@dantownsend Hi. Will it ever reach release? Without this fix, it's hard to switch from Django ORM.

Yes, will merge it in once the tests are passing.

@VladislavYar
Copy link

@dantownsend Hi. Will it ever reach release? Without this fix, it's hard to switch from Django ORM.

Yes, will merge it in once the tests are passing.

@dantownsend Hi. Will it ever reach release? Without this fix, it's hard to switch from Django ORM.

Yes, will merge it in once the tests are passing.

Thanks. I'm sorry for writing the same message twice - I'm a bit of a dumbass:)

Have you considered taking the TableMeta class in the Table at the class level itself, rather than binding it rigidly in the method?
If something needs to be changed in TableMeta, you have to copy-paste the Table.__init_subclass__ method with the indication of your own TableMeta.

@sinisaos
Copy link
Member

@dantownsend Just one question. Shouldn't this be much simpler without the match_db_column_name function parameter and the extra table to test? Something like this:

# your modified code without match_db_column_name param
column_object = next(
   (
       i
       for i in self.columns
       if (i._meta.name == column_name)
       or (i._meta.db_column_name == column_name)
   ),
   None,
)
if column_object is None:
   raise ValueError(f"No matching column found with name == {name}")

and test like this:

# in tests/columns/test_db_column_name.py
def test_prefetch(self):
    """
    Make sure that foreign keys with a ``db_column_name`` work
    with prefetch objects.

    https://github.com/piccolo-orm/piccolo/issues/1107

    """
    manager = Manager.objects().create(name="Guido").run_sync()
    self.insert_band(manager=manager)

    band = (
        Band.objects(Band.manager)
        .where(Band.manager == manager.id)
        .first()
        .run_sync()
    )

    self.assertEqual(
        band.to_dict(),
        {
            "id": band.id,
            "name": "Pythonistas",
            "popularity": 1000,
            "manager": {"id": manager.id, "name": "Guido"},
        },
    )

Sorry if I'm wrong and don't understand this correctly but this is just suggestion.

@dantownsend
Copy link
Member Author

@dantownsend Just one question. Shouldn't this be much simpler without the match_db_column_name function parameter and the extra table to test? Something like this:

# your modified code without match_db_column_name param
column_object = next(
   (
       i
       for i in self.columns
       if (i._meta.name == column_name)
       or (i._meta.db_column_name == column_name)
   ),
   None,
)
if column_object is None:
   raise ValueError(f"No matching column found with name == {name}")

and test like this:

# in tests/columns/test_db_column_name.py
def test_prefetch(self):
    """
    Make sure that foreign keys with a ``db_column_name`` work
    with prefetch objects.

    https://github.com/piccolo-orm/piccolo/issues/1107

    """
    manager = Manager.objects().create(name="Guido").run_sync()
    self.insert_band(manager=manager)

    band = (
        Band.objects(Band.manager)
        .where(Band.manager == manager.id)
        .first()
        .run_sync()
    )

    self.assertEqual(
        band.to_dict(),
        {
            "id": band.id,
            "name": "Pythonistas",
            "popularity": 1000,
            "manager": {"id": manager.id, "name": "Guido"},
        },
    )

Sorry if I'm wrong and don't understand this correctly but this is just suggestion.

Yeah, I need a table with a FK with db_column_name defined. I added a table like that to the playground too, as it's good for manual testing. My main laptop is being repaired, so setting up a new local dev environment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

using db_column_name with ForeignKey breaks objects().select(...) when using prefetch

3 participants