logo
down
shadow

SQLAlchemy: order_by(None) for joinedload subclause queries?


SQLAlchemy: order_by(None) for joinedload subclause queries?

By : user2955089
Date : November 22 2020, 10:38 AM
hope this fix your issue [copying from my answer on the mailing list]
You either need to take order_by off the relationship(), probably the best idea here if ordering isn’t important, or otherwise skip joinedload(), write out the joins yourself and use contains_eager() (http://docs.sqlalchemy.org/en/rel_0_9/orm/loading_relationships.html?highlight=contains_eager#contains-eager).
code :


Share : facebook icon twitter icon
SQLAlchemy: filter many-to-many joinedload

SQLAlchemy: filter many-to-many joinedload


By : Tal Y.
Date : March 29 2020, 07:55 AM
I wish this helpful for you I think contains_eager() is what you're looking for:
code :
words = db_session.query(Word).\
    join(Word.phrases).\
    options(contains_eager(Word.phrases)).\
    filter(Word.id.in_(word_ids)).\
    filter(Phrase.active == True)
SQLAlchemy Joinedload filter column

SQLAlchemy Joinedload filter column


By : meisuzney
Date : March 29 2020, 07:55 AM
To fix the issue you can do The reason it is not working is that joinedload (and all the other relationship loading techniques) are meant to be entirely transparent. That is to say having a joinedload in your query should not affect it in any other way other than resulting in the relationships being filled. You should read "The Zen of Joined Eager Loading", which begins with:
code :
session.query(Work).\
    join(Work.company_users).\
    join(CompanyUser.user).\
    filter(Work.id == 1).\
    filter(User.first_name == 'The name').\
    all()
session.query(Work).\
    join(Work.company_users).\
    join(CompanyUser.user).\
    options(contains_eager(Work.company_users).
            contains_eager(CompanyUser.user)).\
    filter(Work.id == 1).\
    filter(User.first_name == 'The name').\
    all()
SQLAlchemy: Unnecessary join with joinedload

SQLAlchemy: Unnecessary join with joinedload


By : Sebastian Quevedo
Date : March 29 2020, 07:55 AM
Hope this helps It is by design – read The Zen of Joined Eager Loading:
code :
session.query(Author).\
    outerjoin(Author.books, Book.pages).\
    options(contains_eager(Author.books).contains_eager(Book.pages))
Recursive CTE with subclause in SQLAlchemy

Recursive CTE with subclause in SQLAlchemy


By : sumit kumar
Date : March 29 2020, 07:55 AM
I hope this helps you . The current query does not reference the CTE in the recursive step at all. You need an alias of the CTE itself in order to fix that:
code :
In [5]: top_query = db.session.query(NodeParentRelation.child_id,
   ...:                              NodeParentRelation.parent_id)\
   ...:                          .filter(NodeParentRelation.child_id == 1)\
   ...:                          .cte(recursive=True)
   ...:                              

In [6]: parents = db.aliased(top_query)

In [7]: t = db.aliased(NodeParentRelation)

In [11]: query = top_query.union_all(
    ...:     db.session.query(t.child_id, t.parent_id).
    ...:         join(parents, t.child_id == parents.c.parent_id))
In [14]: db.session.query(query.c.parent_id).\
    ...:     filter(query.c.parent_id.notin_(
    ...:         db.session.query(NodeParentRelation.child_id)))
Out[14]: <sqlalchemy.orm.query.Query at 0x7f56bf55b668>

In [15]: print(_)
WITH RECURSIVE anon_1(child_id, parent_id) AS 
(SELECT node_parent_relation.child_id AS child_id, node_parent_relation.parent_id AS parent_id 
FROM node_parent_relation 
WHERE node_parent_relation.child_id = ? UNION ALL SELECT node_parent_relation_1.child_id AS node_parent_relation_1_child_id, node_parent_relation_1.parent_id AS node_parent_relation_1_parent_id 
FROM node_parent_relation AS node_parent_relation_1 JOIN anon_1 AS anon_2 ON node_parent_relation_1.child_id = anon_2.parent_id)
 SELECT anon_1.parent_id AS anon_1_parent_id 
FROM anon_1 
WHERE anon_1.parent_id NOT IN (SELECT node_parent_relation.child_id AS node_parent_relation_child_id 
FROM node_parent_relation)
In [25]: db.session.query(query.c.parent_id).\
    ...:     filter(~db.session.query(NodeParentRelation).
    ...:         filter(NodeParentRelation.child_id == query.c.parent_id).
    ...:         exists())
    ...:         
Sqlalchemy: joinedload + limit

Sqlalchemy: joinedload + limit


By : mohamed
Date : March 29 2020, 07:55 AM
With these it helps This seems like expected behavior according to The Zen of Eager Loading
Related Posts Related Posts :
  • Appending a column in .csv with Python/Pandas
  • How to change my result directory in Robot framework using RIDE?
  • problem with using pandas to manipulate a big text file in python
  • python-magic module' object has no attribute 'open'
  • Where goes wrong for this High Pass Filter in Python?
  • Why inserting keys in order into a python dict is faster than doint it unordered
  • flann index saving in python
  • Create new instance of list or dictionary without class
  • How can I easily convert FORTRAN code to Python code (real code, not wrappers)
  • Address of lambda function in python
  • Python adding space between characters in string. Most efficient way
  • python http server, multiple simultaneous requests
  • Disguising username & password on distributed python scripts
  • Post GraphQL mutation with Python Requests
  • Why doesnt pandas create an excel file?
  • Rolling comparison between a value and a past window, with percentile/quantile
  • How to avoid repetitive code when defining a new type in python with signature verification
  • How to configure uWSGI in order to debug with pdb (--honour-stdin configuration issue)
  • In Python, how do you execute objects that are functions from a list?
  • Python- Variable Won't Subtract?
  • Processing Power In Python
  • Python 2.7.2 - Cannot import name _random or random from sys
  • Why doesn't the Python sorted function take keyword order instead of reverse?
  • Make a function redirect to other functions depending on a variable
  • get_absolute_url in django-categories
  • Monitoring non-Celery background task with New Relic in Python
  • Feature selection with LinearSVC
  • LSTM - Predicting the same constant values after a while
  • Test the length of elements in a list
  • Django: render radiobutton with 3 columns, cost column must change according to size & quantity selected
  • Python class attributes vs global variable
  • sys.stdout.writelines("hello") and sys.stdout.write("hello")
  • is ndarray faster than recarray access?
  • Python - search through directory trees, rename certain files
  • GAE: How to build a query where a string begins with a value
  • TypeError: __init__() takes at least 2 arguments (1 given)
  • Overriding and customizing "django.contrib.auth.views.login"
  • Django : Redirect to a particular page after login
  • Python search and copy files in directory
  • pretty printing numpy ndarrays using unicode characters
  • Frequent pattern mining in Python
  • How can I make a set of functions that can be used synchronously as well as asynchronously?
  • Convert one dice roll to two dice roll
  • count occourrence in a list
  • Writing an If condition to filter out the first word
  • to read file and compare column in python
  • Install python-numpy in the Virtualenv environment
  • `.select_by_visible_text()` is failed to select element?
  • Unable to send data multiple requests in a single connection — socket error
  • Pandas HDFStore unload dataframe from memory
  • Creating a custom admin view
  • How do you get the user role of the currently logged in user in Ckan?
  • Speed up Numpy Meshgrid Command
  • Python error - name lengths
  • appending text to a global variable
  • Python Mistake - Number of letters in name
  • Searching for a sequence in a text
  • Testing logging output with pytest
  • How do I change my default working directory for Python (Anaconda) on VSCode?
  • .lower() for x in list, not working, but works in another scenario
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org