在flask shell

时间:2017-09-26 09:02:09

标签: flask flask-sqlalchemy

当我尝试在我的烧瓶外壳中导入模型时,我得到一个InvalidRequestError。

它似乎与我的项目< b> init .py 有关,如下所示。当我删除此文件中的from . import models时,我可以在我的烧瓶外壳中导入我的模型没有问题。问题是我需要这个" import"运行我的数据库迁移...

有人可以帮忙吗?

$ 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

1 个答案:

答案 0 :(得分:0)

请参阅github上的this问题。这与您的问题相同,与SQLAlchemy版本有关。