question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SQLA: Multiple relationships to another model with search

See original GitHub issue

If 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:closed
  • Created 8 years ago
  • Comments:14 (13 by maintainers)

github_iconTop GitHub Comments

1reaction
bepetersncommented, Feb 15, 2018

I’m wondering if you’re seeing this work for filters. I get this error when trying to filter across multiple relationships:

File "/home/brian/.virtualenvs/my-project/src/flask-admin/flask_admin/contrib/sqla/tools.py", line 198, in is_hybrid_property
    attr = getattr(last_model, names[i])
AttributeError: 'Mapper' object has no attribute 'location'

I can open a new issue also if you want.

1reaction
mrjoescommented, Apr 20, 2015

Try 'B.a1.name' as a string.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found