piccolo icon indicating copy to clipboard operation
piccolo copied to clipboard

Reverse relations fetching

Open Forceres opened this issue 1 year ago • 11 comments

FEATURE REQUEST: Is it a problem to add reverse relation fetching (conceptual or technical)? It would be nice to see something like that:

   class Student(Table, tablename="students"):
       name = Varchar(length=64)
       teacher_id = ForeignKey(Teacher)

   class Teacher(Table, tablename="teachers"):
       name = Varchar(length=64)
       
   await Teacher.objects(Teacher.all_related())

Forceres avatar Jun 25 '24 09:06 Forceres

@Forceres Here is an old reverse lookup PR that never took off. Here is also workaround to get same result with existing code.

sinisaos avatar Jun 25 '24 11:06 sinisaos

Why old PR was never took off?

Forceres avatar Jun 25 '24 11:06 Forceres

@Forceres Here is an old reverse lookup PR that never took off. Here is also workaround to get same result with existing code.

That workaround is too ineffective

Forceres avatar Jun 25 '24 12:06 Forceres

@Forceres That PR was never properly reviewed by the Piccolo author. I don't remember what the reasons were for that (bad API design or something else). Everything worked as expected at the time PR was made. Everything should work now also, but I'm not sure if it does after all the changes that have been made in Piccolo.

sinisaos avatar Jun 25 '24 12:06 sinisaos

@Forceres That PR was never properly reviewed by the Piccolo author. I don't remember what the reasons were for that (bad API design or something else). Everything worked as expected at the time PR was made. Everything should work now also, but I'm not sure if it does after all the changes that have been made in Piccolo.

I don't see any reasons for it to be declined, the only one solution without that PR is to use raw queries builder (written from zero to hero)

Forceres avatar Jun 25 '24 12:06 Forceres

I don't see any reasons for it to be declined, the only one solution with that PR is to use raw queries builder (written from zero to hero)

I agree with you, but you have to wait @dantownsend for an explanation. That PR is pretty similar to how the Piccolo M2M was built.

sinisaos avatar Jun 25 '24 13:06 sinisaos

I don't see any reasons for it to be declined, the only one solution with that PR is to use raw queries builder (written from zero to hero)

I agree with you, but you have to wait @dantownsend for an explanation. That PR is pretty similar to how the Piccolo M2M was built.

Someone has already tried to wait for him) But, anyway, there is no choice, forced to wait

Forceres avatar Jun 25 '24 14:06 Forceres

Modern ORM, which doesn't have basic functionality like reverse relations

Forceres avatar Jun 25 '24 14:06 Forceres

I know it's frustrating when PRs don't get merged in, but it's just a matter of resources and time - I generally have to prioritise stuff I need for client work, as that's what pays my bills. Loads of my own PRs haven't been merged in either, because unless I'm 100% happy with it, and willing to support a feature in the future, I'm hesitant about merging it in.

I do come back to old PRs - so if they haven't been merged they're not 'dead'.

But I agree, this would be a very nice feature to have. I generally work around it with something like this:

teacher = await Teacher.objects().get(name="Alice")
students = await Student.objects().where(Student.teacher == teacher)

dantownsend avatar Jun 26 '24 16:06 dantownsend

I know it's frustrating when PRs don't get merged in, but it's just a matter of resources and time - I generally have to prioritise stuff I need for client work, as that's what pays my bills. Loads of my own PRs haven't been merged in either, because unless I'm 100% happy with it, and willing to support a feature in the future, I'm hesitant about merging it in.

I do come back to old PRs - so if they haven't been merged they're not 'dead'.

But I agree, this would be a very nice feature to have. I generally work around it with something like this:

teacher = await Teacher.objects().get(name="Alice")
students = await Student.objects().where(Student.teacher == teacher)

Got you. Hope you find time to resolve it. That approach has one problem - it is 2 queries instead of one with joins

Forceres avatar Jun 26 '24 19:06 Forceres

I know it's frustrating when PRs don't get merged in, but it's just a matter of resources and time - I generally have to prioritise stuff I need for client work, as that's what pays my bills. Loads of my own PRs haven't been merged in either, because unless I'm 100% happy with it, and willing to support a feature in the future, I'm hesitant about merging it in.

I do come back to old PRs - so if they haven't been merged they're not 'dead'.

But I agree, this would be a very nice feature to have. I generally work around it with something like this:

teacher = await Teacher.objects().get(name="Alice")
students = await Student.objects().where(Student.teacher == teacher)

Any news?

Forceres avatar Jul 04 '24 14:07 Forceres