Sqlalchemy 'str' object (ColumnElement) has no attribute 'label'

Hi,
I'm migrating my SQLAlchemy development from Plone 3.3.5 to Plone 5.2
Have used collective.lead and advices from Datenbankanbindungen — Plone-Entwicklerhandbuch

The transformation to z3c.sqlalchemy 1.5 seems to be ok. I get connection and session and can read metadata und tables.

The mentioned problem occur when a query is started:

place_list = session.query(Place.name.label('name'), Place.id.label('id'), Place.type.label('place_type'), Rural_District.name.label('rural_district'), Rural_District.alias.label('rural_district_alias'),
).filter(func.lower(Place.name)==func.lower(place_name)
).filter(place.c.id==place_in_rd.c.place_id
).filter(place_in_rd.c.district_id==rural_district.c.id
)

Data has been modeled via zope.schema, i.e. Place and Rural_District, as shown in Datenmodellierung — Plone-Entwicklerhandbuch

class IPlace(Interface):
"""Place
"""
id = schema.Int(title=(u"Place identifier"),
description=
(u"A unique id for this place"),
required=True
)
name = schema.TextLine(title=(u"Place name"),
description=
(u"The name for this place (last known)"),
required=True
)
type = schema.TextLine(title=(u"Place type"),
description=
(u"The type of this place (last known)"),
required=True
)

In Plone 3.3.5 with Python 2.4 and SQLAlchemy-0.4.8 the label method has done in the way as above presented.

Now with Plone 5.2, Python 2.7 and SQLAlchemy-1.2.19 (and z3c.sqlalchemy and zope.sqlalchemy) it seems, that Place.name etc. is no longer interpreted as ColumnElement as defined in sqlalchemy/sql/elements.py but as a simple string.

E.g. Place is initialized by:
class Place(object):
implements(IPlace)
id = None
name = ''
type = ''

What do I need to change, that e.g. Place.name will not be interpreted as str but as ColumnElement, or what else is the recommendation.

Regards, Fritz

For those, who may will come into same trouble.
I have changed all into declarative base, which means that zope.schema is not longer used and the mappers declaration from "Plone Entwicklerhandbuch" (see article above) is also not longer necessary.

All other python scripts for sqlalchemy can then be used as in the earlier version.

How to write the declarative base see tutorial for SQLAlchemy.

1 Like