In Sqlalchemy, what's different in defining table using Base and Table

后端 未结 1 1485
醉酒成梦
醉酒成梦 2021-02-19 11:21

I know i can define a table using Table:

user = Table(\'user\', metadata,
    Column(\'user_id\', Integer, primary_key = True),
)

and using Bas

1条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-19 11:50

    Base = declarative_base()
    
    class User(Base):
        __tablename__ = 'user'
    
        id = Column('id', Integer, primary_key=True)
        name = Column('name', Unicode(64))
    

    is just syntactic sugar for

    metadata = MetaData()
    
    user = Table('user', metadata,
        Column('id', Integer, primary_key=True),
        Column('name', Unicode(64)),
    )
    
    class User(object):
        pass
    
    mapper(User, user)  # this will modify the User class!
    

    0 讨论(0)
提交回复
热议问题