Skip to content

Unable to specify name argument to aliased with an association table #5750

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

Closed
funseiki opened this issue Dec 7, 2020 · 2 comments
Closed
Labels
bug Something isn't working orm regression something worked and was broken by a change
Milestone

Comments

@funseiki
Copy link

funseiki commented Dec 7, 2020

Describe the bug

When trying to use aliased with an association table and setting the 'name' argument:

at_alias = aliased(association_table, name="myJoinedTable")

An assertion error appears

sqlalchemy/sql/coercions.py", line 922, in _post_coercion
    assert name is None

Expected behavior
Allowing a name parameter to be set for association tables (unless I'm missing something from the migration notes, in which case sorry!).

To Reproduce

from sqlalchemy import create_engine, Column, Integer, String, DateTime, ForeignKey, Table, MetaData
from sqlalchemy.orm import sessionmaker, relationship, aliased, backref, registry

mapper_reg = registry()
engine = create_engine('sqlite:///:memory:', echo=False)
metadata = MetaData(bind=engine)
association_table = Table('association', metadata,
                          Column('parent_id', Integer, ForeignKey('things.id')),
                          Column('child_id', Integer, ForeignKey('things.id'))
                          )

things = Table('things', metadata,
               Column('id', Integer, primary_key=True))

class Thing(object):
    pass

mapper_reg.map_imperatively(Thing, things,
       properties={
           'children': relationship(Thing,
                                    secondary=association_table,
                                    primaryjoin=things.c.id == association_table.c.parent_id,
                                    secondaryjoin=things.c.id == association_table.c.child_id)
       })

metadata.create_all(engine)

# Below is where the error occurs
at_alias = aliased(association_table, name="myJoinedTable")

Error

Traceback (most recent call last):
  File "aliased_error.py", line 27, in <module>
    at_alias = aliased(association_table, name="myJoinedTable")
  File "/Users/me/.pyenv/versions/2.7.14/lib/python2.7/site-packages/sqlalchemy/orm/util.py", line 1138, in aliased
    roles.AnonymizedFromClauseRole, element, name=name, flat=flat
  File "/Users/me/.pyenv/versions/2.7.14/lib/python2.7/site-packages/sqlalchemy/sql/coercions.py", line 176, in expect
    **kw
  File "/Users/me/.pyenv/versions/2.7.14/lib/python2.7/site-packages/sqlalchemy/sql/coercions.py", line 922, in _post_coercion
    assert name is None
AssertionError

Versions.

  • OS: macOS 10.15.7
  • Python: 2.7.14
  • SQLAlchemy: 1.4.0b1
  • Database: SQLite

Have a nice day!

@funseiki funseiki added the requires triage New issue that requires categorization label Dec 7, 2020
@sqla-tester
Copy link
Collaborator

Mike Bayer has proposed a fix for this issue in the master branch:

Pass through plain selectable to .alias() https://gerrit.sqlalchemy.org/c/sqlalchemy/sqlalchemy/+/2402

@zzzeek zzzeek added bug Something isn't working orm regression something worked and was broken by a change and removed requires triage New issue that requires categorization labels Dec 7, 2020
@zzzeek zzzeek added this to the 1.4 milestone Dec 7, 2020
@zzzeek
Copy link
Member

zzzeek commented Dec 7, 2020

use table.alias(name=name) for now

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 regression something worked and was broken by a change
Projects
None yet
Development

No branches or pull requests

3 participants