Generating models for Flask-AppBuilder using flask-sqlqcodegen

情到浓时终转凉″ 提交于 2019-12-11 06:59:51

问题


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.


回答1:


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. 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

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.



来源:https://stackoverflow.com/questions/38657634/generating-models-for-flask-appbuilder-using-flask-sqlqcodegen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!