SQLA: Multiple relationships to another model with search
See original GitHub issueIf there’s more than one relationship to another model then (I believe) there’s no way to add them to search.
class A(Base):
__tablename__ = 'a'
id = sa.Column(sa.Integer, primary_key=True)
name = sa.Column(sa.Text)
class B(Base):
__tablename__ = 'b'
a1_id = sa.Column(sa.Integer, sa.ForeignKey(A.id))
a1 = orm.relationship(A.id, foreign_keys=[a1_id])
a2_id = sa.Column(sa.Integer, sa.ForeignKey(A.id))
a2 = orm.relationship(A.id, foreign_keys=[a2_id])
...
class BModelView(ModelView):
...
column_searchable_list = (
B.a1.name,
)
# AttributeError: Neither 'InstrumentedAttribute' object nor 'Comparator' object associated with B.a1 has an attribute 'name'
class BModelView(ModelView):
...
column_searchable_list = (
B.a1, # (or the string "a1")
)
# Exception: Invalid field B.a1: does not contains any columns.
class BModelView(ModelView):
...
column_searchable_list = (
A.name,
)
# sqlalchemy.exc.InvalidRequestError: Could not find a FROM clause to join from. Tried joining to a, but got: Can't determine join between 'b' and 'a'; tables have more than one foreign key constraint relationship between them. Please specify the 'onclause' of this join explicitly.
How can we add support for this? Would be nice with an API like this:
class BModelView(ModelView):
...
column_searchable_list = (
# (local_relationship, foreign_attribute)
(B.a1, A.name),
)
Issue Analytics
- State:
- Created 8 years ago
- Comments:14 (13 by maintainers)
Top Results From Across the Web
Search based on many to many relation with multiple ...
One option is to use multiple whereHas as: $query = Job::query(); foreach ($params['tags'] as $tag) { $query->whereHas('tags', function ($q) use($tag) ...
Read more >Relationships in SQL - Complete Guide With Examples
The article provides a detailed guide on types of relationship in a database with examples of how to create different types of relationships...
Read more >Creating multiple tables and table relationships - Launch School
In this chapter we'll explore the reasons for having multiple tables in a database, look at how to define relationships between different tables, ......
Read more >Relationships between tables in a Data Model
A relationship is a connection between two tables of data, based on one column in each. A workbook can store each piece of...
Read more >Eloquent: Relationships - The PHP Framework For Web Artisans
Like all other Eloquent relationships, one-to-many relationships are defined by defining a method on your Eloquent model: <?php. namespace App\Models;.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I’m wondering if you’re seeing this work for filters. I get this error when trying to filter across multiple relationships:
I can open a new issue also if you want.
Try
'B.a1.name'
as a string.