\$\begingroup\$
\$\endgroup\$
2
I want to create a family tree where the relationships are only godfather and godson. Is this the right way to declare the personn class ?
from sqlalchemy import Column, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from genealogie.extensions import db
Base = declarative_base()
relationship = db.Table('relationship', Base.metadata,
Column('godfather', db.Integer, ForeignKey('personn.id'), primary_key=True),
Column('godson', db.Integer, ForeignKey('personn.id'), primary_key=True))
class Personn(db.Model):
__tablename__ = 'personn'
id = Column('id', db.Integer, primary_key=True)
first_name = Column('first_name', db.String(255), nullable=False)
last_name = Column('last_name', db.String(255), nullable=False)
birthdate = Column('birthdate', db.DateTime, nullable=True)
promo = Column('promo', db.Integer, nullable=False)
description = Column('description', db.Text, nullable=True)
godfather = db.relationship('Personn',
secondary=relationship,
primaryjoin=relationship.c.godfather==id,
secondaryjoin=relationship.c.godson==id,
backref="godson")
Vogel612
25.5k7 gold badges59 silver badges141 bronze badges
-
1\$\begingroup\$ soo ... how does your database cover the possibility of multiple godparents? Or of multiple godchildren? \$\endgroup\$Vogel612– Vogel6122022年02月01日 14:41:39 +00:00Commented Feb 1, 2022 at 14:41
-
\$\begingroup\$ With the relationship table \$\endgroup\$louisld– louisld2022年02月03日 08:59:41 +00:00Commented Feb 3, 2022 at 8:59
1 Answer 1
\$\begingroup\$
\$\endgroup\$
Is this the right way to declare the personn class ?
Yes. It looks good to me.
Ship it!
answered Apr 28, 2024 at 4:57
lang-py