2
\$\begingroup\$

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
asked Feb 1, 2022 at 7:58
\$\endgroup\$
2
  • 1
    \$\begingroup\$ soo ... how does your database cover the possibility of multiple godparents? Or of multiple godchildren? \$\endgroup\$ Commented Feb 1, 2022 at 14:41
  • \$\begingroup\$ With the relationship table \$\endgroup\$ Commented Feb 3, 2022 at 8:59

1 Answer 1

1
\$\begingroup\$

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
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.