21

我知道我可以使用 Table 定义一个表:

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

并使用基础:

Base = declarative_base()    
class User(Base):
        __tablename__ = 'user'
        user_id= Column(Integer, primary_key = True)

但有什么不同???

4

1 回答 1

13
Base = declarative_base()

class User(Base):
    __tablename__ = 'user'

    id = Column('id', Integer, primary_key=True)
    name = Column('name', Unicode(64))

只是语法糖

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!
于 2015-10-16T09:01:15.387 回答