2017-09-26 49 views
0

當試圖在我的瓶殼中導入模型時,我得到一個InvalidRequestError。在瓶殼中導入Flask模型時出現InvalidRequestError

它似乎與我的項目的初始 .py有關,如下所示。當我在這個文件中刪除from . import models時,我可以將我的模型導入到我的燒瓶外殼中,沒有任何問題。問題是我需要這個「導入」來運行我的數據庫遷移...

任何人都可以提供幫助嗎?

$ flask shell 

Python 3.6.2 |Continuum Analytics, Inc.| (default, Jul 20 2017, 13:51:32) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux 
IPython: 6.2.0 
App: audiolabeling 
Instance: /home/jul/dev/audiolabeling/instance 
In [1]: from models import TagType 
--------------------------------------------------------------------------- 
InvalidRequestError      Traceback (most recent call last) 
<ipython-input-1-ebb7905a9c4e> in <module>() 
----> 1 from models import TagType 

~/dev/audiolabeling/audiolabeling/models.py in <module>() 
    13 
    14 
---> 15 class TagType(db.Model): 
    16 
    17  id = db.Column(db.Integer, primary_key=True) 

~/.miniconda3/envs/audiolabeling/lib/python3.6/site-packages/flask_sqlalchemy/__init__.py in __init__(self, name, bases, d) 
    600  def __init__(self, name, bases, d): 
    601   bind_key = d.pop('__bind_key__', None) or getattr(self, '__bind_key__', None) 
--> 602   DeclarativeMeta.__init__(self, name, bases, d) 
    603 
    604   if bind_key is not None and hasattr(self, '__table__'): 

~/.miniconda3/envs/audiolabeling/lib/python3.6/site-packages/sqlalchemy/ext/declarative/api.py in __init__(cls, classname, bases, dict_) 
    62  def __init__(cls, classname, bases, dict_): 
    63   if '_decl_class_registry' not in cls.__dict__: 
---> 64    _as_declarative(cls, classname, cls.__dict__) 
    65   type.__init__(cls, classname, bases, dict_) 
    66 

~/.miniconda3/envs/audiolabeling/lib/python3.6/site-packages/sqlalchemy/ext/declarative/base.py in _as_declarative(cls, classname, dict_) 
    86   return 
    87 
---> 88  _MapperConfig.setup_mapping(cls, classname, dict_) 
    89 
    90 

~/.miniconda3/envs/audiolabeling/lib/python3.6/site-packages/sqlalchemy/ext/declarative/base.py in setup_mapping(cls, cls_, classname, dict_) 
    101   else: 
    102    cfg_cls = _MapperConfig 
--> 103   cfg_cls(cls_, classname, dict_) 
    104 
    105  def __init__(self, cls_, classname, dict_): 

~/.miniconda3/envs/audiolabeling/lib/python3.6/site-packages/sqlalchemy/ext/declarative/base.py in __init__(self, cls_, classname, dict_) 
    129   self._extract_declared_columns() 
    130 
--> 131   self._setup_table() 
    132 
    133   self._setup_inheritance() 

~/.miniconda3/envs/audiolabeling/lib/python3.6/site-packages/sqlalchemy/ext/declarative/base.py in _setup_table(self) 
    393      tablename, cls.metadata, 
    394      *(tuple(declared_columns) + tuple(args)), 
--> 395      **table_kw) 
    396   else: 
    397    table = cls.__table__ 

~/.miniconda3/envs/audiolabeling/lib/python3.6/site-packages/sqlalchemy/sql/schema.py in __new__(cls, *args, **kw) 
    419      "to redefine " 
    420      "options and columns on an " 
--> 421      "existing Table object." % key) 
    422    table = metadata.tables[key] 
    423    if extend_existing: 

InvalidRequestError: Table 'tag_type' is already defined for this MetaData instance. Specify 'extend_existing=True' to redefine options and columns on an existing Table object. 

models.py

from audiolabeling import db 
import enum 


class FeedBackType(enum.Enum): 
    NONE = "none" 
    HIDDENIMAGE = "hiddenImage" 


class VisualizationType(enum.Enum): 
    WAVEFORM = "waveform" 
    SPECTROGRAM = "spectrogram" 


class TagType(db.Model): 

    id = db.Column(db.Integer, primary_key=True) 
    name = db.Column(db.String, unique=True) 
    annotationtags = db.relationship('AnnotationTag', 
            backref='tagtype', 
            lazy='dynamic') 

    def __repr__(self): 
     return '<id {}>'.format(self.id) 


class Task(db.Model): 

    id = db.Column(db.Integer, primary_key=True) 
    feedback = db.Column(db.Enum(FeedBackType)) 
    annotationtags = db.relationship('AnnotationTag', 
            backref='task', 
            lazy='dynamic') 
    name = db.Column(db.String, unique=True) 

    def __repr__(self): 
     return '<id {}>'.format(self.id) 


class AnnotationTag(db.Model): 

    id = db.Column(db.Integer, primary_key=True) 
    tagtype_id = db.Column(db.Integer, db.ForeignKey(TagType.id)) 
    task_id = db.Column(db.Integer, db.ForeignKey(Task.id)) 
    name = db.Column(db.String, unique=True) 
    annotations = db.relationship('Annotation', 
            backref='annotationtag', 
            lazy='dynamic') 

    def __repr__(self): 
     return '<id {}>'.format(self.id) 


class Audio(db.Model): 

    id = db.Column(db.Integer, primary_key=True) 
    url = db.Column(db.String, unique=True) 
    tagtype_id = db.Column(db.Integer, db.ForeignKey(TagType.id)) 
    task_id = db.Column(db.Integer, db.ForeignKey(Task.id)) 
    name = db.Column(db.String, unique=True) 
    annotations = db.relationship('Annotation', 
            backref='audio', 
            lazy='dynamic') 

    def __repr__(self): 
     return '<id {}>'.format(self.id) 


class Annotation(db.Model): 

    id = db.Column(db.Integer, primary_key=True) 
    annotationtag_id = db.Column(db.Integer, db.ForeignKey(AnnotationTag.id)) 
    audio_id = db.Column(db.Integer, db.ForeignKey('audio.id')) 

    def __repr__(self): 
     return '<id {}>'.format(self.id) 

初始化的.py

from flask import Flask 
from flask_sqlalchemy import SQLAlchemy 
from flask_migrate import Migrate 


app = Flask(__name__, instance_relative_config=True) 
app.config.from_pyfile('flask.cfg') 


db = SQLAlchemy(app) 
migrate = Migrate(app, db) 

from . import models # OK IF I REMOVE THIS 
from . import views 

回答

0

在Github上查看this問題。這與您的問題相同,並且與SQLAlchemy版本有關。