使用flask-sqlqcodegen为Flask-AppBuilder生成模型 [英] Generating models for Flask-AppBuilder using flask-sqlqcodegen

查看:130
本文介绍了使用flask-sqlqcodegen为Flask-AppBuilder生成模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Python和Flask生态系统的初学者,试图为一个研究项目创建一个小的概念验证Web应用程序.我正在使用Debian Linux 7.9,PostgreSQL 9.5,SQLAlchemy(最新)和Flask-AppBuilder(最新).由于手动创建模型很繁琐且容易出错,因此我搜索了功能强大的Internet并发现了flask-sqlacodegen项目(请注意,这是sqlacodegen的一个分支,为Flask用户提供了改进的功能).我从GitHub安装了flask-sqlqcodegen(克隆了仓库,然后运行了python setup.py install).但是,当尝试使用它生成模型时,会产生错误,如下所示:

I'm a beginner in Python and Flask ecosystems, trying to create a small proof-of-concept Web application for a research project. I'm using Debian Linux 7.9, PostgreSQL 9.5, SQLAlchemy (latest) and Flask-AppBuilder (latest). Since creating models manually is tedious and error-prone, I searched the mighty Internet and discovered the flask-sqlacodegen project (note that this a fork of sqlacodegen with improved features for Flask users). I installed flask-sqlqcodegen from GitHub (cloned repo and then ran python setup.py install). However, when trying to use it to generate models, it produces an error, as follows:

> sqlacodegen postgresql+psycopg2://USER:PASS@HOST/DBNAME --flask

Traceback (most recent call last):
  File "/usr/local/bin/sqlacodegen", line 9, in <module>
    load_entry_point('sqlacodegen==1.1.5.pre2', 'console_scripts', 'sqlacodegen')()
  File "/usr/local/lib/python2.7/dist-packages/sqlacodegen-1.1.5.pre2-py2.7.egg/sqlacodegen/main.py", line 57, in main
    args.flask, fkcols)
  File "/usr/local/lib/python2.7/dist-packages/sqlacodegen-1.1.5.pre2-py2.7.egg/sqlacodegen/codegen.py", line 597, in __init__
    model = ModelClass(table, links[table.name], inflect_engine, not nojoined)
  File "/usr/local/lib/python2.7/dist-packages/sqlacodegen-1.1.5.pre2-py2.7.egg/sqlacodegen/codegen.py", line 319, in __init__
    relationship_ = ManyToOneRelationship(self.name, target_cls, constraint, inflect_engine)
  File "/usr/local/lib/python2.7/dist-packages/sqlacodegen-1.1.5.pre2-py2.7.egg/sqlacodegen/codegen.py", line 455, in __init__
    colname = constraint.columns[0]
  File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/util/_collections.py", line 194, in __getitem__
    return self._data[key]
KeyError: 0

这是怎么回事?任何帮助将不胜感激.

What is going on? Any help will be much appreciated.

推荐答案

在一些Internet搜索中,我在GitHub上遇到了一个问题,该问题描述了完全相同的问题.但是,当时的最新建议产生了另一个错误,而不是原始错误.在与flask-sqlcodegen的作者一起的讨论中,似乎存在一个由项目贡献者提供的请求请求(PR),显然可以解决该问题.更新本地存储库,然后重建并重新安装软件之后,我能够为数据库成功生成模型.整个过程包括以下步骤.

Upon some Internet searching, I ran across an issue on GitHub, which described exactly the same problem. However, the most recent recommendation at the time produced another error instead of the original one. In the discussion with the author of flask-sqlcodegen, it appeared that there exist a pull request (PR) kindly provided by a project contributor that apparently should fix the problem. After updating my local repository, followed by rebuilding and reinstalling the software, I was able to successfully generate models for my database. The whole process consists of the following steps.

  1. 更改为本地仓库为flask-sqlcodegen的目录.
  2. 如果您像我一样进行了任何更改,请保存它们:git stash.
  3. 更新存储库:git pull origin master(现在包括该PR).
  4. 重建/安装软件:python setup.py install.
  5. 如果需要之前的更改,请还原它们:git stash pop.否则,将其丢弃:git reset --hard.
  6. 更改到Flask应用程序目录并自动生成模型,如下所示.

  1. Change to directory with a local repo of flask-sqlcodegen.
  2. If you made any changes, like I did, stash them: git stash.
  3. Update repo: git pull origin master (now includes that PR).
  4. Rebuild/install software: python setup.py install.
  5. If you need your prior changes, restore them: git stash pop. Otherwise, discard them: git reset --hard.
  6. Change to your Flask application directory and auto-generate the models, as follows.

sqlacodegen --flask --outfile models.py postgresql+psycopg2://USER:PASS@HOST/DBNAME

致谢:非常感谢Kamil Sindi(flask-sqlcodegen的作者)提供了出色的软件和快速的&有用的反馈,以及对Alisdair Venn提出的宝贵请求.

Acknowledgements: Big thank you to Kamil Sindi (the flask-sqlcodegen's author) for the nice software and rapid & helpful feedback as well as to Alisdair Venn for that valuable pull request.

这篇关于使用flask-sqlqcodegen为Flask-AppBuilder生成模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆