{"id":100072,"date":"2020-04-29T16:03:05","date_gmt":"2020-04-29T08:03:05","guid":{"rendered":"http:\/\/4563.org\/?p=100072"},"modified":"2020-04-29T16:03:05","modified_gmt":"2020-04-29T08:03:05","slug":"%e5%85%b3%e4%ba%8e-flask-sqlalchemy-%e7%9a%84%e8%b7%a8-db-%e6%9f%a5%e8%af%a2","status":"publish","type":"post","link":"http:\/\/4563.org\/?p=100072","title":{"rendered":"\u5173\u4e8e flask-sqlalchemy \u7684\u8de8 db \u67e5\u8be2"},"content":{"rendered":"<div>\n<div>\n<div>\n<h1>                  \u5173\u4e8e flask-sqlalchemy \u7684\u8de8 db \u67e5\u8be2               <\/h1>\n<p> <\/p>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : mashpolo <\/span>  <span><i><\/i> 63<\/span> <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<\/p><\/div>\n<div isfirst=\"1\"> <\/p>\n<p>\u4eca\u5929\u78b0\u5230\u4e00\u4e2a\u68d8\u624b\u7684\u95ee\u9898\uff0c\u4e00\u4e2a\u63a5\u53e3\u9700\u8981\u540c\u65f6\u67e5\u8be2\u4e24\u4e2a db \u7684\u4e24\u4e2a\u8868\uff0c\u4f46\u5176\u4e2d\u4e00\u4e2a\u8868\uff0csqlalchemy \u67e5\u627e\u7684 db \u662f\u53e6\u5916\u4e00\u4e2a\uff0c\u5bfc\u81f4\u67e5\u8be2\u627e\u4e0d\u5230\u8be5\u8868<\/p>\n<p>model \u4ee3\u7801\u5982\u4e0b<\/p>\n<pre><code>class BusinessGroup(db.Model):     __bind_key__ = 'amp'     __tablename__ = 'business_group'      business_group_id = Column(db.BigInteger, primary_key=True)      class DeployJob(Base):     __tablename__ = 'deploy_job'      id = Column(BIGINT(20), primary_key=True) <\/code><\/pre>\n<p>\u6570\u636e\u5e93\u8fde\u63a5\uff1a<\/p>\n<pre><code> SQLALCHEMY_BINDS = {'amp': 'mysql+pymysql:\/\/{user}:{passwd}@{host}:{port}\/{db}'.format(**AMP_MYSQL_CFG)} <\/code><\/pre>\n<p>\u67e5\u8be2\u7684\u63a5\u53e3\u4ee3\u7801<\/p>\n<pre><code>def getDpJobList():     try:         deploy_jobs_basic = db.session.query(DeployJob.id,                                              BusinessGroup.dept_id)  <\/code><\/pre>\n<p>\u5f97\u5230\u7684\u9519\u8bef\uff1a<\/p>\n<pre><code>(pymysql.err.ProgrammingError) (1146, \"Table 'monitor.deploy_job' doesn't exist\") <\/code><\/pre>\n<p>\u4e0d\u77e5\u9053\u6709\u77e5\u9053\u8be5\u95ee\u9898\u6240\u5728\u7684\u5c0f\u4f19\u4f34\u6ca1\uff1f\u6c42\u6307\u5bfc\u3002 \u591a\u8c22<\/p>\n<\/p><\/div>\n<div> <b>\u5927\u4f6c\u6709\u8a71\u8aaa<\/b> (<span>7<\/span>)        <\/div>\n<div> <\/div>\n<\/p><\/div>\n<\/p><\/div>\n<ul>\n<li data-pid=\"774362\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : yzongyue <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e24\u4e2a db (schema) \u4e0d\u5728\u4e00\u4e2a db \u4e0a\uff1f\u90a3\u6ca1\u529e\u6cd5\u4e86\uff0c\u5206\u522b\u67e5\u51fa\u6765 python \u5904\u7406\u4e0b\u5427                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"774363\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : mashpolo <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @yzongyue #1 \u770b\u6587\u6863\u5e94\u8be5\u662f\u53ef\u4ee5\u7684\uff0c\u4f7f\u7528`__bind_key__`\u53ef\u4ee5\u5b9a\u4e49\u53e6\u5916\u7684 model\uff0c\u4f46\u6211\u8fd9\u4e2a\uff0c\u4e0d\u77e5\u9053\u662f\u54ea\u51fa\u95ee\u9898\u4e86\u3002\u67e5\u8be2\u975e bind \u7684 table \u7684\u65f6\u5019\uff0c\u7ed3\u679c\u627e\u5230 bind \u7684 db \u4e2d\u53bb\u4e86                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"774364\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : outoftimeerror <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u76f4\u63a5 presto \u5427                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"774365\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : yzongyue <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @mashpolo bind key \u53ef\u4ee5\u7ed1\u5b9a\u4e0d\u540c\u7684 db\uff0c\u4f46 python \u4ee3\u7801\u6700\u7ec8\u8fd8\u662f\u8981\u8f6c\u6362\u6210 sql\uff0c\u4e0d\u5728\u4e00\u4e2a db \u5f53\u7136\u4e0d\u80fd\u4e00\u4e2a sql \u67e5\u51fa\u6765                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"774366\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : tairan2006 <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u76f4\u63a5\u88f8\u7528 sqlalchemy\uff0c\u65b0\u5efa\u4e00\u4e2a engine\u2026                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"774367\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u8cc7\u6df1\u5927\u4f6c : whoosy <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             \u4e24\u4e2a db \u4e0d\u80fd\u7528\u540c\u4e00\u4e2a session\uff0c\u4e5f\u5c31\u662f\u4e0d\u80fd\u7528\u4e00\u4e2a db.session, \u4f46\u662f\u53ef\u4ee5\u518d\u5f00\u4e00\u4e2a db.session \u53bb\u67e5                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li data-pid=\"774368\" data-uid=\"2\">\n<div>\n<div>\n<div> <span>\u4e3b<\/span> <span>\u8cc7\u6df1\u5927\u4f6c : mashpolo <\/span>  <\/div>\n<div> <i title=\"\u5f15\u7528\"><\/i>  <span>          <\/span> <\/div>\n<\/p><\/div>\n<div>                                                             @yzongyue #4 <br \/>@tairan2006 #5 <br \/>@whoosy #6 \u4e86\u89e3\u4e86\uff0c\u770b\u6765\u786e\u5b9e\u4e0d\u884c\uff0c\u6211\u4fee\u6539\u4e0b\u8bbe\u8ba1\u7684\u601d\u8def                                                            <\/div>\n<\/p><\/div>\n<\/li>\n<li>\n","protected":false},"excerpt":{"rendered":"<p>\u5173\u4e8e flask-sqlalche&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[],"tags":[],"_links":{"self":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/100072"}],"collection":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=100072"}],"version-history":[{"count":0,"href":"http:\/\/4563.org\/index.php?rest_route=\/wp\/v2\/posts\/100072\/revisions"}],"wp:attachment":[{"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=100072"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=100072"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/4563.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=100072"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}