Columns incorrectly renamed when using label_length
The code below will fail when a column name in ModelA is longer than "label_length". This happens in all versions after 0.7.5.
The sql generated is:
SELECT tbl_b.id AS _1, tbl_b.a_id AS _2, _1.id AS _3, _1._5 AS _4
FROM tbl_b LEFT OUTER JOIN tbl_a AS _1 ON _1.id = tbl_b.a_id
"_1._5 AS _4" should be "_.1.abcde AS _4"
import sqlalchemy as sql
from sqlalchemy import orm, Column as C
from sqlalchemy.ext.declarative import declarative_base
BaseModel = declarative_base()
class ModelA(BaseModel):
__tablename__ = 'tbl_a'
id = C(sql.Integer, primary_key=True)
abcde = C(sql.String)
class ModelB(BaseModel):
__tablename__ = 'tbl_b'
id = C(sql.Integer, primary_key=True)
a_id = C(sql.Integer, sql.ForeignKey(ModelA.id))
modela = orm.relationship(ModelA)
engine = sql.create_engine('sqlite://', label_length=4)
BaseModel.metadata.bind = engine
BaseModel.metadata.create_all()
Session = orm.sessionmaker(bind=engine)
ses = Session()
m_a = ModelA()
m_b = ModelB(modela=m_a)
ses.add(m_b)
ses.commit()
ses.query(ModelB).options(orm.joinedload(ModelB.modela)).all()
Comments (5)
-
repo owner -
repo owner also, the reason this case isn't covered is that the original assumption of label_length (and possibly requirement, I need to re-acquaint myself with the code here) was that it's only used to satisfy the max identifier length of the target database, from which it follows that no column name would ever be outside of that range.
-
repo owner - changed milestone to 0.8.0final
the attached patch fixes this and passes all tests. some cleanup will be needed and tests for the new functionality, and we may want to modernize test_labels also.
This is best left in the 0.8 series for now. If there are major issues with that let me know.
-
repo owner - changed status to resolved
-
repo owner - removed milestone
Removing milestone: 0.8.0final (automated comment)
- Log in to comment
here's a test
and here's a patch that doesn't work elsewhere but illustrates where the tinkering will be needed