When an object is inserted into the database using SQLAlchemy, all its properties corresponding to the String () columns are automatically converted from <type 'str'> to <type 'unicode'>. Is there a way to prevent this behavior?
Here is the code:
from sqlalchemy import create_engine, Table, Column, Integer, String, MetaData from sqlalchemy.orm import mapper, sessionmaker engine = create_engine('sqlite:///:memory:', echo=False) metadata = MetaData() table = Table('projects', metadata, Column('id', Integer, primary_key=True), Column('name', String(50)) ) class Project(object): def __init__(self, name): self.name = name mapper(Project, table) metadata.create_all(engine) session = sessionmaker(bind=engine)() project = Project("Lorem ipsum") print(type(project.name)) session.add(project) session.commit() print(type(project.name))
And here is the conclusion:
<type 'str'> <type 'unicode'>
I know that I probably just need to work with unicode, but this will require digging through some third-party code, and I still don't have Python skills for this :)
source share