ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# flask中使用sqlalchemy处理多对多 ``` <pre class="calibre29">``` <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 #首先创建三张表 #最后创建中间表 article_tag <span class="token">=</span> db<span class="token1">.</span><span class="token3">Table</span><span class="token1">(</span><span class="token4">'article_tag'</span><span class="token1">,</span>db<span class="token1">.</span><span class="token3">Column</span><span class="token1">(</span><span class="token4">'article_id'</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">'article.id'</span><span class="token1">)</span><span class="token1">,</span>primary_key<span class="token">=</span>True<span class="token1">)</span><span class="token1">,</span> db<span class="token1">.</span><span class="token3">Column</span><span class="token1">(</span><span class="token4">'tag_id'</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">'tag.id'</span><span class="token1">)</span><span class="token1">,</span>primary_key<span class="token">=</span>True<span class="token1">)</span><span class="token1">)</span> #第一创建article表 <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> #两张表进行关联 tags <span class="token">=</span> db<span class="token1">.</span><span class="token3">relationship</span><span class="token1">(</span><span class="token4">'Tag'</span><span class="token1">,</span>secondary <span class="token">=</span>article_tag<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> #第二创建标签表 <span class="token2">class</span> <span class="token3">Tag</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">'tag'</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> name <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> #标签名字 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> <span class="token3">Article</span><span class="token1">(</span>title <span class="token">=</span> <span class="token4">'aaa'</span><span class="token1">)</span> article2 <span class="token">=</span> <span class="token3">Article</span><span class="token1">(</span>title <span class="token">=</span> <span class="token4">'bbb'</span><span class="token1">)</span> tag1 <span class="token">=</span> <span class="token3">Tag</span><span class="token1">(</span>name <span class="token">=</span> <span class="token4">'111'</span><span class="token1">)</span> tag2<span class="token">=</span> <span class="token3">Tag</span><span class="token1">(</span>name <span class="token">=</span> <span class="token4">'222'</span><span class="token1">)</span> #对文章添加标签 article1 <span class="token1">.</span>tags<span class="token1">.</span><span class="token3">append</span><span class="token1">(</span>tag1<span class="token1">)</span> article1 <span class="token1">.</span>tags<span class="token1">.</span><span class="token3">append</span><span class="token1">(</span>tag2<span class="token1">)</span> article2 <span class="token1">.</span>tags<span class="token1">.</span><span class="token3">append</span><span class="token1">(</span>tag1<span class="token1">)</span> article2 <span class="token1">.</span>tags<span class="token1">.</span><span class="token3">append</span><span class="token1">(</span>tag2<span class="token1">)</span> db<span class="token1">.</span>session<span class="token1">.</span><span class="token3">add</span><span class="token1">(</span>article1<span class="token1">)</span> db<span class="token1">.</span>session<span class="token1">.</span><span class="token3">add</span><span class="token1">(</span>article2<span class="token1">)</span> db<span class="token1">.</span>session<span class="token1">.</span><span class="token3">add</span><span class="token1">(</span>tag1<span class="token1">)</span> db<span class="token1">.</span>session<span class="token1">.</span><span class="token3">add</span><span class="token1">(</span>tag2<span class="token1">)</span> db<span class="token1">.</span>session<span class="token1">.</span><span class="token3">commit</span><span class="token1">(</span><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> ``` ``` 查看多对多数据 ``` <pre class="calibre14">``` <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 #首先创建三张表 #最后创建中间表 article_tag <span class="token">=</span> db<span class="token1">.</span><span class="token3">Table</span><span class="token1">(</span><span class="token4">'article_tag'</span><span class="token1">,</span>db<span class="token1">.</span><span class="token3">Column</span><span class="token1">(</span><span class="token4">'article_id'</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">'article.id'</span><span class="token1">)</span><span class="token1">,</span>primary_key<span class="token">=</span>True<span class="token1">)</span><span class="token1">,</span> db<span class="token1">.</span><span class="token3">Column</span><span class="token1">(</span><span class="token4">'tag_id'</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">'tag.id'</span><span class="token1">)</span><span class="token1">,</span>primary_key<span class="token">=</span>True<span class="token1">)</span><span class="token1">)</span> #第一创建article表 <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> #两张表进行关联 tags <span class="token">=</span> db<span class="token1">.</span><span class="token3">relationship</span><span class="token1">(</span><span class="token4">'Tag'</span><span class="token1">,</span>secondary <span class="token">=</span>article_tag<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> #第二创建标签表 <span class="token2">class</span> <span class="token3">Tag</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">'tag'</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> name <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> #标签名字 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> <span class="token3">Article</span><span class="token1">(</span>title <span class="token">=</span> <span class="token4">'aaa'</span><span class="token1">)</span> # article2 <span class="token">=</span> <span class="token3">Article</span><span class="token1">(</span>title <span class="token">=</span> <span class="token4">'bbb'</span><span class="token1">)</span> # # tag1 <span class="token">=</span> <span class="token3">Tag</span><span class="token1">(</span>name <span class="token">=</span> <span class="token4">'111'</span><span class="token1">)</span> # tag2<span class="token">=</span> <span class="token3">Tag</span><span class="token1">(</span>name <span class="token">=</span> <span class="token4">'222'</span><span class="token1">)</span> # # #对文章添加标签 # article1 <span class="token1">.</span>tags<span class="token1">.</span><span class="token3">append</span><span class="token1">(</span>tag1<span class="token1">)</span> # article1 <span class="token1">.</span>tags<span class="token1">.</span><span class="token3">append</span><span class="token1">(</span>tag2<span class="token1">)</span> # article2 <span class="token1">.</span>tags<span class="token1">.</span><span class="token3">append</span><span class="token1">(</span>tag1<span class="token1">)</span> # article2 <span class="token1">.</span>tags<span class="token1">.</span><span class="token3">append</span><span class="token1">(</span>tag2<span class="token1">)</span> # # db<span class="token1">.</span>session<span class="token1">.</span><span class="token3">add</span><span class="token1">(</span>article1<span class="token1">)</span> # db<span class="token1">.</span>session<span class="token1">.</span><span class="token3">add</span><span class="token1">(</span>article2<span class="token1">)</span> # db<span class="token1">.</span>session<span class="token1">.</span><span class="token3">add</span><span class="token1">(</span>tag1<span class="token1">)</span> # db<span class="token1">.</span>session<span class="token1">.</span><span class="token3">add</span><span class="token1">(</span>tag2<span class="token1">)</span> # db<span class="token1">.</span>session<span class="token1">.</span><span class="token3">commit</span><span class="token1">(</span><span class="token1">)</span> #查看标题是aa的有哪些文章 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">'aaa'</span><span class="token1">)</span><span class="token1">.</span><span class="token3">first</span><span class="token1">(</span><span class="token1">)</span> tags <span class="token">=</span> article1<span class="token1">.</span>tags <span class="token2">for</span> tag <span class="token2">in</span> tags<span class="token1">:</span> <span class="token3">print</span><span class="token1">(</span>tag<span class="token1">.</span>name <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> ``` ```