<p dir="auto">Following the <a href="https://fastapi.tiangolo.com/tutorial/sql-databases/" rel="nofollow">SQL (Relational) Databases tutorial</a>, it seems one has to define Pydantic and SQLAlchemy models separately:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="# models.py
class User(Base):
    __tablename__ = 'users'
    id = Column(Integer, primary_key=True)
    name = Column(String)

# schema.py
class User(BaseModel):
    name: str"><pre class="notranslate"><span class="pl-c"># models.py</span>
<span class="pl-k">class</span> <span class="pl-v">User</span>(<span class="pl-v">Base</span>):
    <span class="pl-s1">__tablename__</span> <span class="pl-c1">=</span> <span class="pl-s">'users'</span>
    <span class="pl-s1">id</span> <span class="pl-c1">=</span> <span class="pl-v">Column</span>(<span class="pl-v">Integer</span>, <span class="pl-s1">primary_key</span><span class="pl-c1">=</span><span class="pl-c1">True</span>)
    <span class="pl-s1">name</span> <span class="pl-c1">=</span> <span class="pl-v">Column</span>(<span class="pl-v">String</span>)

<span class="pl-c"># schema.py</span>
<span class="pl-k">class</span> <span class="pl-v">User</span>(<span class="pl-v">BaseModel</span>):
    <span class="pl-s1">name</span>: <span class="pl-s1">str</span></pre></div>
<p dir="auto">As the number of models grows, this leads to large amounts of duplicate code and attributes. Is there a way to merge these two classes into one, or are there any plans on adding such functionality?</p>
<h3 dir="auto">Describe the bug</h3>
<p dir="auto">"startup" and "shutdown" events not triggered for mounted APIs</p>
<h3 dir="auto">To Reproduce</h3>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="from fastapi import FastAPI
from fastapi.testclient import TestClient

app = FastAPI()
subapp = FastAPI()
app.mount(&quot;/prefix&quot;, subapp)

@app.on_event(&quot;startup&quot;)
async def startup_event_1() -&gt; None:
    print(&quot;startup for&quot;, app)

@app.on_event(&quot;shutdown&quot;)
async def shutdown_event_1() -&gt; None:
    print(&quot;shutdown for&quot;, app)

@subapp.on_event(&quot;startup&quot;)
async def startup_event_2() -&gt; None:
    print(&quot;startup for&quot;, subapp)

@subapp.on_event(&quot;shutdown&quot;)
async def shutdown_event_2() -&gt; None:
    print(&quot;shutdown for&quot;, subapp)

with TestClient(app):
    pass"><pre class="notranslate"><span class="pl-k">from</span> <span class="pl-s1">fastapi</span> <span class="pl-k">import</span> <span class="pl-v">FastAPI</span>
<span class="pl-k">from</span> <span class="pl-s1">fastapi</span>.<span class="pl-s1">testclient</span> <span class="pl-k">import</span> <span class="pl-v">TestClient</span>

<span class="pl-s1">app</span> <span class="pl-c1">=</span> <span class="pl-v">FastAPI</span>()
<span class="pl-s1">subapp</span> <span class="pl-c1">=</span> <span class="pl-v">FastAPI</span>()
<span class="pl-s1">app</span>.<span class="pl-en">mount</span>(<span class="pl-s">"/prefix"</span>, <span class="pl-s1">subapp</span>)

<span class="pl-en">@<span class="pl-s1">app</span>.<span class="pl-en">on_event</span>(<span class="pl-s">"startup"</span>)</span>
<span class="pl-k">async</span> <span class="pl-k">def</span> <span class="pl-en">startup_event_1</span>() <span class="pl-c1">-&gt;</span> <span class="pl-c1">None</span>:
    <span class="pl-en">print</span>(<span class="pl-s">"startup for"</span>, <span class="pl-s1">app</span>)

<span class="pl-en">@<span class="pl-s1">app</span>.<span class="pl-en">on_event</span>(<span class="pl-s">"shutdown"</span>)</span>
<span class="pl-k">async</span> <span class="pl-k">def</span> <span class="pl-en">shutdown_event_1</span>() <span class="pl-c1">-&gt;</span> <span class="pl-c1">None</span>:
    <span class="pl-en">print</span>(<span class="pl-s">"shutdown for"</span>, <span class="pl-s1">app</span>)

<span class="pl-en">@<span class="pl-s1">subapp</span>.<span class="pl-en">on_event</span>(<span class="pl-s">"startup"</span>)</span>
<span class="pl-k">async</span> <span class="pl-k">def</span> <span class="pl-en">startup_event_2</span>() <span class="pl-c1">-&gt;</span> <span class="pl-c1">None</span>:
    <span class="pl-en">print</span>(<span class="pl-s">"startup for"</span>, <span class="pl-s1">subapp</span>)

<span class="pl-en">@<span class="pl-s1">subapp</span>.<span class="pl-en">on_event</span>(<span class="pl-s">"shutdown"</span>)</span>
<span class="pl-k">async</span> <span class="pl-k">def</span> <span class="pl-en">shutdown_event_2</span>() <span class="pl-c1">-&gt;</span> <span class="pl-c1">None</span>:
    <span class="pl-en">print</span>(<span class="pl-s">"shutdown for"</span>, <span class="pl-s1">subapp</span>)

<span class="pl-k">with</span> <span class="pl-v">TestClient</span>(<span class="pl-s1">app</span>):
    <span class="pl-k">pass</span></pre></div>
<p dir="auto">startup/shutdown is triggered for <code class="notranslate">app</code> but not <code class="notranslate">subapp</code></p>
<h3 dir="auto">Expected behavior</h3>
<p dir="auto">Events are emitted in both cases</p>