Skip to content

Instantly share code, notes, and snippets.

@jacobsvante
Created April 23, 2015 09:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jacobsvante/122fb2272bd7feb83b31 to your computer and use it in GitHub Desktop.
Save jacobsvante/122fb2272bd7feb83b31 to your computer and use it in GitHub Desktop.
Test case for flask-admin/flask-admin issue #846 (and usage of PR #808)
from flask import Flask
import sqlalchemy as sa
from sqlalchemy import orm
from sqlalchemy.ext.declarative import declarative_base
from flask.ext.admin import Admin
from flask.ext.admin.contrib import sqla
app = Flask(__name__)
engine = sa.create_engine(
'sqlite://',
connect_args={'check_same_thread': False},
poolclass=sa.pool.StaticPool,
)
session = orm.scoped_session(orm.sessionmaker())
metadata = sa.MetaData()
Base = declarative_base(metadata=metadata, bind=engine)
Base.query = session.query_property()
# Create models
class A(Base):
__tablename__ = 'a'
__str__ = lambda s: s.name
id = sa.Column(sa.Integer, primary_key=True)
name = sa.Column(sa.Text)
class B(Base):
__tablename__ = 'b'
__str__ = lambda s: s.title
id = sa.Column(sa.Integer, primary_key=True)
title = sa.Column(sa.Text)
a1_id = sa.Column(sa.Integer, sa.ForeignKey(A.id))
a1 = sa.orm.relationship(A, foreign_keys=[a1_id])
a2_id = sa.Column(sa.Integer, sa.ForeignKey(A.id))
a2 = sa.orm.relationship(A, foreign_keys=[a2_id])
Base.metadata.create_all()
xyz = A(name='xyz')
bar = A(name='bar')
foobar = B(title='foobar', a1=xyz, a2=bar)
session.add(xyz)
session.add(bar)
session.add(foobar)
session.commit()
# Customized Post model admin
class BAdmin(sqla.ModelView):
column_searchable_list = (
# A.name,
# Doing a search raises:
# 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.
# Using PR #808 (commit cd90dfd) doesn't give this exception, but also doesn't yield any search results
# B.a1.name,
# App crashes with:
# AttributeError: Neither 'InstrumentedAttribute' object nor 'Comparator' object associated with B.a1 has an attribute 'name'
# B.a1,
# App crashes with:
# Exception: Invalid field B.a1: does not contains any columns.
# 'a1.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.
# http://localhost:5000/admin/b/?search=xyz works with PR #808 and gives the correct search result.
# (https://github.com/flask-admin/flask-admin/pull/808, commit cd90dfd)
'a1.name',
# http://localhost:5000/admin/b/?search=bar does not give any results for PR #808, why is that?
# 'a2.name',
)
admin = Admin()
admin.init_app(app)
admin.add_view(sqla.ModelView(A, session))
admin.add_view(BAdmin(B, session))
app.run(debug=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment