| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Sorry, something went wrong.
|
⚠️ Please install the Codecov ReportAttention: Patch coverage is 90.07634% with 13 lines in your changes missing coverage. Please review.
❗ Your organization needs to install the Codecov GitHub app to enable full functionality. Additional details and impacted files@@ Coverage Diff @@
## master #599 +/- ##
==========================================
+ Coverage 88.42% 92.54% +4.12%
==========================================
Files 117 118 +1
Lines 8653 8772 +119
==========================================
+ Hits 7651 8118 +467
+ Misses 1002 654 -348 ☔ View full report in Codecov by Sentry. |
Sorry, something went wrong.
|
Thanks for this! It looks very promising. |
Sorry, something went wrong.
|
Thanks. I used a lot of code (with some modifications) from M2M fields. I tried to use the best names of variables and methods. The main obstacle is that the user would have to enter reverse_lookup_name. I will try to make that optional (if table has only one FK default index value for foreign_key_columns will be 0), so that the user has to specify reverse_lookup_name only if the table has multiple FK so ReverseLookup knows which column to use. If you have any ideas, any help is welcome. |
Sorry, something went wrong.
|
Just wanted to note that there is interest in this functionality. Is there anything specific holding this back (from being merged)? Also I like how in peewee one can just set the backref parameter on the ForeignKeyField, see here |
Sorry, something went wrong.
I haven't had chance to review it yet - but I agree that it's a feature we need. |
Sorry, something went wrong.
|
@dantownsend It would be great if you could find the time to review this. @powellnorma This code works and passes all tests. If you think that things should be changed, it would be best after Daniel's review. Thank you both. |
Sorry, something went wrong.
I haven't looked, but I guess the tests always use a ForeignKey named the same as the referenced Table. As e.g. here: class Manager(Table):
name = Varchar()
class Artist(Table):
name = Varchar()
manager = ForeignKey(Manager)But if this is not true, as e.g. here: class MainManager(Table):
name = Varchar()
class Artist(Table):
name = Varchar()
manager = ForeignKey(MainManager)the sections I pointed lead to errors / wrong queries |
Sorry, something went wrong.
|
When I query an M2M field of the referenced table, I get: AttributeError: type object 'M2MSelect' has no attribute 'value_type' |
Sorry, something went wrong.
Make it work when the ForeignKey is named differently than the referenced Table.
|
I have fixed the parts that I reviewed on my branch reverse_lookup, see: https://github.com/powellnorma/piccolo/commits/reverse_lookup However querying an M2M field (of the referenced table) seems to be rather slow at the moment. When I set self.serialisation_safe = False in the case of the above mentioned AttributeError, it seems that all m2m joining keys are being used as arguments to a second query. This is overall (in my case) slower than using a for loop in python (which is located in a transaction) instead of the ReverseLookup. |
Sorry, something went wrong.
I'm sorry, but I don't understand. How can we refer to a table that doesn't exist. Shouldn't it be like this class MainManager(Table):
name = Varchar()
class Artist(Table):
name = Varchar()
manager = ForeignKey(MainManager)I want to try your branch. In my reverse lookup attempt I use LazyTableReference like this class Manager(Table):
name = Varchar()
bands = ReverseLookup(
LazyTableReference("Band", module_path=__name__),
)
class Band(Table):
name = Varchar()
manager = ForeignKey(Manager)
# query for all managers bands
query = await Manager.select(
Manager.name, Manager.bands(Band.name, as_list=True, table=Manager)
)Can you show me how to write a table schema and an example query because right now I get TypeError: __init__() missing 1 required positional argument: 'reverse_fk'. Thanks in advance |
Sorry, something went wrong.
The example I gave was wrong, I corrected it. What I meant was a situation in which the column name (of the foreign key) has a different name than the table that it refers to
Like this: class MainManager(Table):
name = Varchar()
artists = ReverseLookup(LazyTableReference("Artist", module_path=__name__), "manager")
class Artist(Table):
name = Varchar()
manager = ForeignKey(MainManager)
# select:
await MainManager.select(MainManager.artists(Artist.name)) |
Sorry, something went wrong.
You are right, it doesn't work if the column is different from the table name. I tried to use foreign_key_columns_index so that if the table has multiple fk columns we know the index of those columns, but it obviously doesn't work well.
This also doesn't work. Raise ValueError: _name isn't defined - the Table Metaclass should set it. on line 148 name property. |
Sorry, something went wrong.
Hm, for me that example works. I (already) made changes so that _name should (indeed) be set from the Table Metaclass here. |
Sorry, something went wrong.
|
Sorry, that was my mistake. I tried everything from scratch because I probably messed something up and you are right, the tests from my PR pass with your code. |
Sorry, something went wrong.
|
@sinisaos I created a PR on your fork/branch. If you accept it, my changes should be included in this PR.
Do you have an example that demonstrates this problem? How can I reproduce this? Thank you |
Sorry, something went wrong.
|
Hey, any updates on this feature ? I still use the PR, but would be great to have in the master piccolo orm. As far i can see the PR is still doing great job in terms of integration over all this year piccolo's updates. Best |
Sorry, something went wrong.
|
@dantownsend Please it has been 3years now. Can you share your feedbacks on this or if uncomfortable the reasons this patch isn't yet applied in the master branch ? Thank you |
Sorry, something went wrong.
|
@sam2x I'm glad you find reverse lookup useful. I've created a branch that I update regularly (to keep it in sync with the master branch). This branch is not official and I know is not perfect, but it does contain some important features that are not in the main branch. |
Sorry, something went wrong.
|
@dantownsend Can you please take a look at this PR as well, because from the comments I see that people are using this without any issues (even after 3.5 years). Thanks. |
Sorry, something went wrong.
|
Seconded, this would be very helpful. Could this work with M2M too? |
Sorry, something went wrong.
|
@snow-born You can already use reverse lookup in M2M (this PR is heavily inspired by M2M, but for O2M relationships) because Piccolo M2M is basically wrapper around efficient SQL with joins (I think that's great because we use databases and SQL which is always more efficient). You almost always have two tables and one joining table so you can easily go in both directions. |
Sorry, something went wrong.
|
I'm closing this PR just to clean up my open requests in the repo. Since it has been open for quite a while, I'll remove it from the list to keep things clean. Feel free to use it if needed. Thanks. |
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
Related to #378. Prototype for reverse foreign key lookup. Any suggestions and help are welcome. If I missed the point or it's not a good api design feel free to close PR.
Usage of reverse FK lookup: