如何向自动映射的SqlAlchemy类添加函数?

时间:2017-04-26 13:23:04

标签: python python-3.x sqlalchemy

我想知道是否可以通过以下方式使用SqlAlchemy automap_base()

from sqlalchemy.ext.automap import automap_base
from sqlalchemy import create_engine

# automap base
Base = automap_base()

# pre-declare User for the 'user' table
class User(Base):
    __tablename__ = 'user'

    def __str__(self):
        return self.name

    def greet(self):
        return 'Hello {}!'.format(self.name)

    def add_address(self, address):
        '''Utilizes the auto reflected relationship to add
           a new address with a proper user_id to table "addresses"'''
        self.address_collection.append(address)

# reflect
engine = create_engine("sqlite:///mydatabase.db")
# only to generate attributes and relationships
Base.prepare(engine, reflect=True)

所以我想反映数据库中的数据和关系,但是扩展了自动生成的类以及其他功能。有可能吗?

0 个答案:

没有答案