4

我正在开发一个小型数据库,其中人员远多于用户,因此目前有以下模型。

class Person(db.Model):
  __tablename__ = 'people'
  id       = db.Column(db.Integer, primary_key = True)
  forename = db.Column(db.String(64))
  surname  = db.Column(db.String(64))

  memberships = db.relationship('Membership', backref='person')

  @property
  def name(self):
    return self.forename + ' ' + self.surname

  def __repr__(self):
    return '<Person %r %r>' % (self.forename, self.surname)

class User(Person):
  __tablename__ = 'users'
  id       = db.Column(db.Integer, db.ForeignKey('people.id'), primary_key = True)
  email    = db.Column(db.String(120), index = True, unique = True)
  role     = db.Column(db.SmallInteger, default = ROLE_USER)

  salt     = db.Column(db.BINARY(8))
  password = db.Column(db.BINARY(20))

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

它工作得很好,如果我创建一个用户,那么一个人也会得到保存。问题是当数据库中已经存在一个人时创建一个用户。

我尝试了以下方法:

>>> p = models.Person.query.get(3)
>>> u = models.User(id=p.id, email="example@example.com")
>>> u.set_password('password')
>>> db.session.add(u)
>>> db.session.commit()
Traceback
...
sqlalchemy.exc.IntegrityError: (IntegrityError) PRIMARY KEY must be unique u'INSERT INTO people (id, forename, surname) VALUES (?, ?, ?)' (3, None, None)

我究竟做错了什么?有没有办法从一个人创建一个用户?

4

1 回答 1

1

Michael Bayer 的建议下,我改变了多态性设计并改用了组合。代码如下...

class Person(db.Model):
  __tablename__ = 'people'
  id       = db.Column(db.Integer, primary_key = True)
  forename = db.Column(db.String(64))
  surname  = db.Column(db.String(64))

  memberships = db.relationship('Membership', backref='person')

  @property
  def name(self):
    return self.forename + ' ' + self.surname

  def __repr__(self):
    return '<Person %r %r>' % (self.forename, self.surname)

class User(db.Model):
  __tablename__ = 'users'
  id       = db.Column(db.Integer, db.ForeignKey('people.id'), primary_key = True)
  email    = db.Column(db.String(120), index = True, unique = True)
  role     = db.Column(db.SmallInteger, default = ROLE_USER)

  salt     = db.Column(db.BINARY(8))
  password = db.Column(db.BINARY(20))

  person   = db.relationship('Person')

与问题中的一个类似的会话将读取...

>>> p = models.Person.query.get(3)
>>> u = models.User(person=p, email="example@example.com")
>>> u.set_password('password')
>>> db.session.add(u)
>>> db.session.commit()
>>>
于 2012-10-29T21:54:17.810 回答