<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Leo Sussan on Medium]]></title>
        <description><![CDATA[Stories by Leo Sussan on Medium]]></description>
        <link>https://medium.com/@lsussan?source=rss-2aecd08204bb------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*iAD8-gDTmYm_5Tp1.jpeg</url>
            <title>Stories by Leo Sussan on Medium</title>
            <link>https://medium.com/@lsussan?source=rss-2aecd08204bb------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 14:16:27 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@lsussan/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Base Classes & One-To-Many Relationships in Flask_SQLAlchemy]]></title>
            <link>https://medium.com/@lsussan/base-classes-one-to-many-relationships-in-flask-sqlalchemy-fba0d47374ad?source=rss-2aecd08204bb------2</link>
            <guid isPermaLink="false">https://medium.com/p/fba0d47374ad</guid>
            <category><![CDATA[sqlalchemy]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[flask-sqlalchemy]]></category>
            <category><![CDATA[beginner-coding]]></category>
            <category><![CDATA[flask]]></category>
            <dc:creator><![CDATA[Leo Sussan]]></dc:creator>
            <pubDate>Wed, 11 Apr 2018 05:32:56 GMT</pubDate>
            <atom:updated>2018-04-11T05:32:56.986Z</atom:updated>
            <content:encoded><![CDATA[<p>Flask_SQLAlchemy is incredible — it handles what would otherwise be a ridiculously complex setup process — but it, like SQLAlchemy on its’ own, has it’s quirks.</p><p>One set of quirks revolve around creating an abstract ‘Base’ class. The purpose is to establish a set of standards for your models, and then produce a prototype from which all of your classes can inherit.</p><p>Why? Well, it leaves you with less duplicate code &amp; reduces your codebase’s complexity.</p><p>I wanted to produce a base class that would handle three simple fields: uuid, created_on and updated_on fields.</p><p>Here’s the setup that seems to work for me (I use PostgreSQL):</p><pre><strong>from </strong>flask_sqlalchemy <strong>import </strong>SQLAlchemy<br><br><strong>from </strong>sqlalchemy.dialects.postgresql.base <strong>import </strong>UUID<br><strong>from </strong>uuid <strong>import </strong>uuid4<br><br>db = SQLAlchemy()<br>db.UUID = UUID<br><br><strong>class </strong>BaseClass(db.Model):<br>    __abstract__ = True<br>    id = db.Column(db.UUID(as_uuid=True), default=<strong>lambda</strong>: uuid4().hex, primary_key=True)<br>    created_on = db.Column(db.DateTime, default=db.func.now())<br>    updated_on = db.Column(db.DateTime, default=db.func.now(), onupdate=db.func.now())</pre><p>There are a couple of important things here:</p><ul><li>UUID is not handled directly in the default ‘db’ object. In order for this to work, the db object needs a point of reference, which we establish right after its’ declaration.</li><li>It is <strong>very important </strong>to make sure that you pass <em>as_uuid=True </em>to the db.UUID(as_uuid=True)</li><li>The lambda function listed in the <em>default </em>field is used as a means of circumventing a limitation around UUID in SQLAlchemy.</li></ul><p>Now, here’s what we do with that Base class.</p><pre><strong>class </strong>Person(BaseClass):<br>    __tablename__ = <strong>&#39;people&#39;<br>    </strong>first_name = db.Column(db.Text, nullable=False)<br>    middle_name = db.Column(db.Text)<br>    last_name = db.Column(db.Text, nullable=False)<br>    type = db.Column(db.Text)<br>    email_addresses = db.relationship(<strong>&#39;EmailAddress&#39;</strong>, back_populates=<strong>&#39;person&#39;</strong>, lazy=True)<br>    phone_numbers = db.relationship(<strong>&#39;PhoneNumber&#39;</strong>, back_populates=<strong>&#39;person&#39;</strong>, lazy=True)<br><br><br><strong>class </strong>EmailAddress(BaseClass):<br>    __tablename__ = <strong>&#39;email_addresses&#39;<br>    </strong>email_address = db.Column(db.Text, nullable=False)<br>    person = db.relationship(<strong>&quot;Person&quot;</strong>, back_populates=<strong>&quot;email_addresses&quot;</strong>)<br>    person_id = db.Column(db.UUID, db.ForeignKey(<strong>&#39;people.id&#39;</strong>))<br><br><br><strong>class </strong>PhoneNumber(BaseClass):<br>    __tablename__ = <strong>&#39;phone_numbers&#39;<br>    </strong>phone_number = db.Column(db.Text, nullable=False)<br>    person = db.relationship(<strong>&quot;Person&quot;</strong>, back_populates=<strong>&quot;phone_numbers&quot;</strong>)<br>    person_id = db.Column(db.UUID, db.ForeignKey(<strong>&#39;people.id&#39;</strong>))</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fba0d47374ad" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>