企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
# python3 sqlalchemy 一对一关系 ``` <pre class="calibre15">``` <span class="token2">from</span> flask <span class="token2">import</span> Flask <span class="token2">from</span> flask_sqlalchemy <span class="token2">import</span> SQLAlchemy <span class="token2">import</span> config #以上导入所需模块 app <span class="token">=</span> <span class="token3">Flask</span><span class="token1">(</span>__name__<span class="token1">)</span> #实例化flask app<span class="token1">.</span>config<span class="token1">.</span><span class="token3">from_object</span><span class="token1">(</span>config<span class="token1">)</span>#引入数据库配置 db <span class="token">=</span> <span class="token3">SQLAlchemy</span><span class="token1">(</span>app<span class="token1">)</span> #实例化sqlalchemy db<span class="token1">.</span><span class="token3">create_all</span><span class="token1">(</span><span class="token1">)</span> #测试观察是否有错误 #Flask<span class="token">-</span>SQLAlchemy外键及其关系 <span class="token2">class</span> <span class="token3">User</span><span class="token1">(</span>db<span class="token1">.</span>Model<span class="token1">)</span><span class="token1">:</span> __tablename__ <span class="token">=</span> <span class="token4">'user'</span> id <span class="token">=</span> db<span class="token1">.</span><span class="token3">Column</span><span class="token1">(</span>db<span class="token1">.</span>Integer<span class="token1">,</span>primary_key<span class="token">=</span>True<span class="token1">,</span>autoincrement<span class="token">=</span>True<span class="token1">)</span> username <span class="token">=</span> db<span class="token1">.</span><span class="token3">Column</span><span class="token1">(</span>db<span class="token1">.</span><span class="token3">String</span><span class="token1">(</span><span class="token5">100</span><span class="token1">)</span><span class="token1">,</span>nullable<span class="token">=</span>False<span class="token1">)</span> <span class="token2">class</span> <span class="token3">Article</span><span class="token1">(</span>db<span class="token1">.</span>Model<span class="token1">)</span><span class="token1">:</span> __tablename__ <span class="token">=</span> <span class="token4">'article'</span> id <span class="token">=</span> db<span class="token1">.</span><span class="token3">Column</span><span class="token1">(</span>db<span class="token1">.</span>Integer<span class="token1">,</span> primary_key<span class="token">=</span>True<span class="token1">,</span> autoincrement<span class="token">=</span>True<span class="token1">)</span> title <span class="token">=</span> db<span class="token1">.</span><span class="token3">Column</span><span class="token1">(</span>db<span class="token1">.</span><span class="token3">String</span><span class="token1">(</span><span class="token5">100</span><span class="token1">)</span><span class="token1">,</span>nullable<span class="token">=</span>False<span class="token1">)</span> content <span class="token">=</span> db<span class="token1">.</span><span class="token3">Column</span><span class="token1">(</span>db<span class="token1">.</span>Text<span class="token1">,</span>nullable<span class="token">=</span>False<span class="token1">)</span> author_id <span class="token">=</span> db<span class="token1">.</span><span class="token3">Column</span><span class="token1">(</span>db<span class="token1">.</span>Integer<span class="token1">,</span>db<span class="token1">.</span><span class="token3">ForeignKey</span><span class="token1">(</span><span class="token4">'user.id'</span><span class="token1">)</span><span class="token1">)</span> author <span class="token">=</span> db<span class="token1">.</span><span class="token3">relationship</span><span class="token1">(</span><span class="token4">'User'</span><span class="token1">,</span>backref<span class="token">=</span>db<span class="token1">.</span><span class="token3">backref</span><span class="token1">(</span><span class="token4">'articles'</span><span class="token1">)</span><span class="token1">)</span> db<span class="token1">.</span><span class="token3">create_all</span><span class="token1">(</span><span class="token1">)</span> @app<span class="token1">.</span><span class="token3">route</span><span class="token1">(</span><span class="token4">'/'</span><span class="token1">)</span> def <span class="token3">index</span><span class="token1">(</span><span class="token1">)</span><span class="token1">:</span> article1 <span class="token">=</span> Article<span class="token1">.</span>query<span class="token1">.</span><span class="token3">filter</span><span class="token1">(</span>Article<span class="token1">.</span>title <span class="token">==</span> <span class="token4">'如何收服韩式'</span><span class="token1">)</span><span class="token1">.</span><span class="token3">first</span><span class="token1">(</span><span class="token1">)</span> <span class="token3">print</span><span class="token1">(</span>article1<span class="token1">.</span>author<span class="token1">.</span>username<span class="token1">)</span> <span class="token2">return</span> <span class="token4">'hello'</span> <span class="token2">if</span> __name__ <span class="token">==</span> <span class="token4">'__main__'</span><span class="token1">:</span> app<span class="token1">.</span><span class="token3">run</span><span class="token1">(</span>debug<span class="token">=</span>True<span class="token1">)</span> ``` ```