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.

evaluate behavior of adding a tuple_() as a column to select(), Query(). coerce? behavior changes on master?

See original GitHub issue

Issue

When trying to select a tuple_:

class User(base):
  __tablename__ = "users"
  uid = Column(Integer, primary_key=True)
  username = Column(String)

[...]

q = session.query(User).add_columns(sqlalchemy.tuple_(User.uid, User.username))
print(q.all())

The returned value for the tuple is a string, which is hardly exploitable: [(<__main__.User object at 0x7fa37c9fce48>, '(1,foo)')]

One requires, I think, with a postgresql backend to test this.

Expected behavior

I would expect to receive a Python tuple [(<__main__.User object at 0x7fa37c9fce48>, (1, 'foo'))] as a result.

Versions

Flask-SQLAlchemy==2.4.0

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:14 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
zzzeekcommented, Jan 31, 2020

this should work on 1.3.x and 1.2.x:

from sqlalchemy.orm.query import _BundleEntity

def add_bundle(bundle):
    def process(q):
        q = q._clone()
        q._entities = list(q._entities)
        _BundleEntity(q, bundle)
        q._set_entity_selectables(q._entities[-1:])
        return q

    return process


q = session.query(User).with_transformation(
    add_bundle(Bundle("mybundle", User.uid, User.username))
)
print(q.all())

0reactions
zzzeekcommented, Aug 15, 2020

since no backend other than PostgreSQL actually allows the syntax of “SELECT (x, y, z) FROM table”, I’ve opted to go with a compile-level raise when the tuple is detected as being placed in the top-most columns clause of the statement. the Bundle implies a non-tuple series of columns in the SELECT that become a sub-row. It’s different and it would be best if, given a tuple_() in the select(), we refuse the temptation to guess.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Functions for Working with Tuples | ClickHouse Docs
A function that allows getting a column from a tuple. ... Example of using a Tuple -type column as the untuple function parameter:...
Read more >
SQL Order by Clause overview and examples
This article will cover the SQL ORDER BY clause including syntax, usage scenarios to sort out results in a Select statement.
Read more >
psycopg2.extras – Miscellaneous goodies for Psycopg 2
New in version 2.3. ... A cursor that generates results as namedtuple . fetch*() methods will return named tuples instead of regular tuples,...
Read more >
Spark SQL, DataFrames and Datasets Guide
A DataFrame is a Dataset organized into named columns. It is conceptually equivalent to a table in a relational database or a data...
Read more >
The Craziness of Subset Selection in pandas — an Edge Case
Oddly enough, tuples are allowable as valid column names in a pandas DataFrame. The KeyError informs us that the tuple (1, 2) is...
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