Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bundle w/ same name columns misbehaves #4295

Closed
sqlalchemy-bot opened this issue Jun 29, 2018 · 3 comments
Closed

Bundle w/ same name columns misbehaves #4295

sqlalchemy-bot opened this issue Jun 29, 2018 · 3 comments
Labels
bug Something isn't working orm
Milestone

Comments

@sqlalchemy-bot
Copy link
Collaborator

Migrated issue, originally created by Michael Bayer (@zzzeek)

from sqlalchemy import *
from sqlalchemy.orm import *
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.declarative import declared_attr

Base = declarative_base()


class A(Base):
    __tablename__ = 'a'

    id = Column(Integer, primary_key=True)
    data = Column(String)
    bs = relationship("B")


class B(A):
    __tablename__ = 'b'

    id = Column(Integer, ForeignKey('a.id'), primary_key=True)


class C(A):
    __tablename__ = 'c'

    c_id = Column(Integer, ForeignKey('a.id'), primary_key=True)

from sqlalchemy.sql.elements import ClauseList

# ClauseList / Bundle are equivalent
print(ClauseList(A.id, C.c_id))
print(Bundle("pk", A.id, C.c_id).__clause_element__())

# w/ same name, they are not because it creates the clause list using *c without using exprs
print(ClauseList(A.id, B.id))
print(Bundle("pk", A.id, B.id).__clause_element__())

output:

a.id, c.c_id
a.id, c.c_id
a.id, b.id
b.id

fix:

diff --git a/lib/sqlalchemy/orm/query.py b/lib/sqlalchemy/orm/query.py
index 98747c680..0b6b32a07 100644
--- a/lib/sqlalchemy/orm/query.py
+++ b/lib/sqlalchemy/orm/query.py
@@ -1738,7 +1738,7 @@ class Query(object):
                 self._group_by = False
                 return
 
-        criterion = list(chain(*[_orm_columns(c) for c in criterion]))
+        criterion = c = list(chain(*[_orm_columns(c) for c in criterion]))
         criterion = self._adapt_col_list(criterion)
 
         if self._group_by is False:
@@ -3964,7 +3964,7 @@ class Bundle(InspectionAttr):
         return cloned
 
     def __clause_element__(self):
-        return expression.ClauseList(group=False, *self.c)
+        return expression.ClauseList(group=False, *self.exprs)
 
     @property
     def clauses(self):

@sqlalchemy-bot
Copy link
Collaborator Author

Michael Bayer (@zzzeek) wrote:

Use exprs for bundle clause_element

Fixed bug in :class:.Bundle construct where placing two columns of the
same name would be de-duplicated, when the :class:.Bundle were used as
part of the rendered SQL, such as in the ORDER BY or GROUP BY of the statement.

Change-Id: Ia528c9fbb399a6beb5ea7cdd3a8a83ad530f5831
Fixes: #4295

2fdf260

@sqlalchemy-bot
Copy link
Collaborator Author

Changes by Michael Bayer (@zzzeek):

  • changed status to closed

@sqlalchemy-bot
Copy link
Collaborator Author

Michael Bayer (@zzzeek) wrote:

Use exprs for bundle clause_element

Fixed bug in :class:.Bundle construct where placing two columns of the
same name would be de-duplicated, when the :class:.Bundle were used as
part of the rendered SQL, such as in the ORDER BY or GROUP BY of the statement.

Change-Id: Ia528c9fbb399a6beb5ea7cdd3a8a83ad530f5831
Fixes: #4295
(cherry picked from commit 2fdf260)

7643e4f

@sqlalchemy-bot sqlalchemy-bot added bug Something isn't working orm labels Nov 27, 2018
@sqlalchemy-bot sqlalchemy-bot added this to the 1.2.x milestone Nov 27, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working orm
Projects
None yet
Development

No branches or pull requests

1 participant