I've written a small tool that creates SQLAlchemy code from a written specification. The tool can create code like this:
class Person(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50))
address_id = db.Column(db.Integer, db.ForeignKey('address.id'))
class Address(db.Model):
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(50))
I can insert new records, I can query them, that works like a charm, but I would really like to not having to manually look for the Address of a Person.
I want to be able to access the address belonging to a specific person through the Person object. If I understand the examples correctly I need to do it like this (example based on http://flask-sqlalchemy.pocoo.org/2.1/models/#one-to-many-relationships)?
class Person(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50))
address = db.relationship('Address', backref='person',
lazy='dynamic')
class Address(db.Model):
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(50))
person_id = db.Column(db.Integer, db.ForeignKey('person.id'))
This looks to me all backwards, I have to give every Address a "link" to a Person and then backref so I can have an addresses for a person? Is this not possible to do directly from Person?
Also, creating code for this is much more complicated, so I'd really like to avoid that.