简体   繁体   English

如何在SQLAlchemy的连接表继承中从父类创建子类?

[英]How do I create a child class from a parent class in SQLAlchemy's Joined Table Inheritance?

I'm developing a small database where there are far more People than Users, so currently have the following Model. 我正在开发一个小型数据库,其中人员远远多于用户,因此目前有以下模型。

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)

It's working quite well, in that if I create a User then a Person also get's saved. 它工作得很好,因为如果我创建一个用户,那么一个人也会得到保存。 The problem is creating a User when a Person already exists in the database. 当数据库中已存在Person时,问题是创建用户。

I have tried the following: 我尝试过以下方法:

>>> 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)

What am I doing wrong? 我究竟做错了什么? Is there a way to create a User from a Person? 有没有办法从人创建用户?

On Michael Bayer's advice , I've changed the polymorphism design and used composition instead. 根据迈克尔拜耳的建议 ,我改变了多态设计并改为使用了构图。 The code reads as follows... 代码如下:

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')

A similar session to the one in the question would read... 与问题中的一个类似的会话将会读到......

>>> 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()
>>>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Sqlalchemy:如何更新连接表继承中的父记录 - Sqlalchemy: How do I update a parent record in a joined table inheritance 从SQLAlchemy联接表继承类继承 - Inherit From SQLAlchemy Joined Table Inheritance Class 如何在具有合并继承的SQLalchemy中从父实例强制转换子实例 - How to cast a child instance from a parent instance in SQLalchemy with joined inheritance 在sqlalchemy中,当子表有多个父表的外键时,如何使用多态连接表继承? - In sqlalchemy, how can I use polymorphic joined table inheritance when the child table has multiple foreign keys to the parent table? 如何创建子级/父级到json的sqlalchemy类? - How to create sqlalchemy class with child/parent to json? Python继承:从子类创建父类对象 - Python inheritance: Create parent class object from child class 如何从 Python 中的子类调用父类的方法? - How do I call a parent class's method from a child class in Python? Python继承:在父类中创建子对象 - Python inheritance: create child object in parent class 我如何正确地将 model 加入表 inheritance 与基于 SQLAlchemy 中的角色的鉴别器 - How do I correctly model joined table inheritance with discriminator based on Role in SQLAlchemy SQLAlchemy - 如何在父级已经持久化时插入连接表继承类实例 - SQLAlchemy - How to insert a Joined Table Inherited class instance when the parent is already persisted
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM