6
class Geolocation(db.Model):
    __tablename__ = "geolocation"
    id = db.Column(db.Integer, primary_key=True)
    latitude = db.Column(db.Float)
    longitude = db.Column(db.Float)
    elevation = db.Column(db.Float)         # Meters
    # Relationships
    pin = db.relationship('Pin', uselist=False, backref="geolocation")

    def __init__(self, latitude, longitude, elevation):
        self.latitude = latitude
        self.longitude = longitude
        self.elevation = elevation

    def __repr__(self):
        return '<Geolocation %s, %s>' % (self.latitude, self.longitude)


class Pin(db.Model):
    __tablename__ = "pin"
    id = db.Column(db.Integer, primary_key=True)
    geolocation_id = db.Column(db.Integer, db.ForeignKey('geolocation.id'))  # True one to one relationship (Implicit child)

    def __init__(self, geolocation_id):
        self.geolocation_id = geolocation_id

    def __repr__(self):
        return '<Pin Object %s>' % id(self)      # Instance id merely useful to differentiate instances.


class User(Pin):
    #id = db.Column(db.Integer, primary_key=True)
    pin_id = db.Column(db.Integer, db.ForeignKey('pin.id'), primary_key=True)
    username = db.Column(db.String(80), unique=True, nullable=False)
    password_hash = db.Column(db.String(120), nullable=False)
    salt = db.Column(db.String(120), nullable=False)
    # Relationships
    #posts = db.relationship('Post', backref=db.backref('user'), lazy='dynamic')               #One User to many Postings.

    def __init__(self, username, password_hash, salt, geolocation_id):
        super(Pin, self).__init__(self, geolocation_id)
        self.username = username
        self.password_hash = password_hash
        self.salt = salt

    def __repr__(self):
        return '<User %r>' % self.username

我对如何在 SQLAlchemy 中设置 id 和与子类的关系感到困惑(我碰巧使用的是 Flask-SQLAlchemy)。我的总体设计是让超类 Pin 成为具有地理位置的任何事物(即用户、地点等)的高级表示。

Pin 和 Geolocation 对象之间存在一对一的关系,因此 Geolocation 不会同时包含两个用户(或用户和地点)的位置。现在我想继承 Pin 来创建 User 类。用户对象应该有一个名称、密码哈希、盐,我还希望能够通过userObj.geolocation. 但是,我后来想创建一个类 Place ,它也是 Pin 的子类,我应该能够通过placeObj.geolocation. 给定一个地理位置对象,我应该可以使用geolocationObj.pin查找用户/地点/等。对应的地理位置对象。我引入超类 Pin 的全部原因是确保 Pin 和 Geolocation 对象之间存在纯粹的一对一关系,而不是让 Geolocation 与需要 Geolocation 表具有user_idplace_id列的 User 或 Person 相关联,其中之一将始终为空。

我希望每个用户都能.geolocation通过父 Pin 类自动拥有一个属性,该属性引用 Geolocation 但似乎 SQLAlchemy 没有这样做。我怎样才能使子类关系起作用以实现我的目标,即让 User 和 Place 以及可能的其他类子类 Pin,让这些类中的每一个都具有通过 Pin 的地理位置属性,并且在 Pin 和地理位置之间具有一对一的关系?

4

2 回答 2

7

我想出的解决方案。这是在声明式风格的 SQLAlchemy 中子类化和使用 Join 继承的完整示例。

class Geolocation(Base):
    __tablename__ = "geolocation"
    id = Column(Integer, primary_key=True)
    latitude = Column(Float)
    longitude = Column(Float)
    elevation = Column(Float)         # Meters
    # Relationships
    person = relationship('Pin', uselist=False, backref="geolocation")

    def __init__(self, latitude, longitude, elevation):
        self.latitude = latitude
        self.longitude = longitude
        self.elevation = elevation

    def __repr__(self):
        return '<Geolocation %s, %s>' % (self.latitude, self.longitude)


class Pin(Base):
    __tablename__ = 'pin'
    id = Column(Integer, primary_key=True)
    geolocation_id = Column(Integer, ForeignKey('geolocation.id'), unique=True, nullable=False)  # True one to one relationship (Implicit child)
    type = Column('type', String(50))              # discriminator
    __mapper_args__ = {'polymorphic_on': type}

    def __init__(self, geolocation_id):
        self.geolocation_id = geolocation_id


class User(Pin):
    __tablename__ = 'user'
    id = Column(Integer, ForeignKey('pin.id'), primary_key=True)
    __mapper_args__ = {'polymorphic_identity': 'user',
                       'inherit_condition': (id == Pin.id)}
    user_id = Column(Integer, autoincrement=True, primary_key=True, unique=True)
    username = Column(String(80), unique=True)
    password_hash = Column(String(120))
    salt = Column(String(120))
    posts = relationship('Posting', primaryjoin="(User.user_id==Posting.user_id)", backref=backref('user'), lazy='dynamic')   #One User to many Postings.

    def __init__(self, username, password_hash, salt, geo_id):
        super(User, self).__init__(geo_id)
        self.username = username
        self.password_hash = password_hash
        self.salt = salt

    def __repr__(self):
        return '<User %s>' % (self.username)


class Posting(Pin):
    __tablename__ = 'posting'
    id = Column(Integer, ForeignKey('pin.id'), primary_key=True)
    __mapper_args__ = {'polymorphic_identity': 'posting',
                        'inherit_condition': (id == Pin.id)}
    posting_id = Column(Integer, autoincrement=True, primary_key=True, unique=True)
    creation_time = Column(DateTime)
    expiration_time = Column(DateTime)
    user_id = Column(Integer, ForeignKey('user.user_id'))              # One User to many Postings

    def __init__(self, creation_time, expiration_time, user_id, geo_id):
        super(Posting, self).__init__(geo_id)
        # For now, require creation time to be passed in. May make this default to current time.
        self.creation_time = creation_time
        self.expiration_time = expiration_time
        self.user_id = user_id

    def __repr__(self):
        #TODO come up with a better representation
        return '<Post %s>' % (self.creation_time)
于 2012-10-17T17:37:58.837 回答
3

这是映射继承层次结构和在 SQLAlchemy 中以声明方式进行的文档。

我相信您会想要连接表继承风格,这意味着您的父类链中的每个类都有自己的表,其中包含唯一的列。基本上,您需要在表中添加一个鉴别器列来pin表示每个 Pin 的子类类型,并为您的类添加一些双下划线属性来描述对 SQLAlchemy 的继承配置。

于 2012-10-16T21:52:17.730 回答