Migrating from SQLAlchemy core to ORM

28 views
Skip to first unread message

Arunabha Ghosh

unread,
Jun 15, 2016, 11:10:44 PM6/15/16
to sqlalchemy
Hi Folks,
               First of all, thanks for creating such a wonderful library. I've been using SQLAlchemy in my first serious Python project and it's the library I wish every language had !

Currently we only use SQLAlchemy core, so all the tables are defined using Table. Now, we would like to incrementally port the existing Tables to ORM and have new Tables use ORM from scratch. 

As an example, say we currently have 

Users = Table(
   
'users', # Table name
    metadata
,
   
Column('id', String, primary_key=True),
   
Column('name', String),
   
Column('email_address', String),
)

and we'd like to add a Posts table using the ORM, something like 

class Posts(Base):
    __tablename__ = 'posts'
    id = Column(Integer, primary_key=True)
    contents = Column(String(300))

How do I update the Users table to reflect the fact that a user can have multiple Posts ? The intended end goal is to have something like

class Users(Base):
    __tablename__ = 'users'
    id = Column(Integer, primary_key=True)
    name = Column(String),
    email_address = Column(String),
    posts = relationship("Posts", back_populates="submitter")

class Posts(Base):
    __tablename__ = 'posts'
    id = Column(Integer, primary_key=True)
    contents = Column(String)
    submitter = relationship("Users", back_populates="posts")


Is it possible to do this ? Or will I have to convert the Users table to ORM before doing something like this ?

Thanks,
Arunabha
Reply all
Reply to author
Forward
0 new messages