I am trying to use reflect feature of sqlalchemy but running into issues.
This is the mysql table i am trying to reflect .
| Country | CREATE TABLE `Country` (
`Code` varchar(8) NOT NULL,
`Country` varchar(64) NOT NULL,
`IsValid` varchar(1) DEFAULT 'Y',
PRIMARY KEY (`Code`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
Reflecting code(sch.py).
from sqlalchemy.ext.declarative import declarative_base
import sqlalchemy
import eng
#Base = sqlalchemy.ext.declarative.declarative_base()
Base = declarative_base()
country = Table('Country',eng.Meta,autoload=True)
class Country(Base):
__tablename__ = country
__mapper_args = {
'primary_key' : [country.c.Code]
}
eng.py
import sqlalchemy
import sqlalchemy.orm
eng = sqlalchemy.create_engine('mysql://user:pass@host/db',echo=True)
Meta = sqlalchemy.MetaData()
Meta.bind = eng
sesDec = sqlalchemy.orm.sessionmaker(bind=eng)
When i import sch
i get error.
import sqlalchemy
from sch import Country
import eng
ses = eng.sesDec()
res = ses.query(Country.Code.label('Code'),Country.Country.label('Country'))
for rec in res.filter(Country.IsValid == 'Y').filter(Country.Code.like('A%')).order_by(Country.Code).all():
print rec
Error(bottom of stack-trace)
File "/pythonlib/lib64/python2.4/site-packages/sqlalchemy/sql/expression.py", line 4198, in description
return self.name.encode('ascii', 'backslashreplace')
AttributeError: 'Table' object has no attribute 'encode
class Country(Base):
__tablename__ = country
Did you mean __table__ = country
?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.