Flask 开发中的SQLAlchemy数据库递归溢出问题


如题,在进行 Flask 开发的时,连接 SQlAlchemy 。进入 shell 准备导入 models 的时候,
出现 RuntimeError: maximum recursion depth exceeded 错误。
googol 之后不少 StackOverFlow 的解决方案是增加一段声明:


 import sys
sys.setrecursionlimit (****)

加入之后没有解决问题,而且不知道怎样探查括号中的值究竟是什么?
图片描述
代码:
__init__.py


 # -*- coding: utf-8 -*-
from flask import Flask
from werkzeug.routing import BaseConverter
from os import path
from flask_bootstrap import Bootstrap
from flask_nav import Nav
from flask_nav.elements import *
from flask_sqlalchemy import SQLAlchemy
from .views import init_views


class RegexConverter(BaseConverter):
    def __init__(self, url_map, *items):
        super(RegexConverter, self).__init__(url_map)
        self.regex = items[0]

basedir = path.abspath(path.dirname(__file__))
bootstrap = Bootstrap()
nav = Nav()
db = SQLAlchemy()


def create_app():
    app = Flask(__name__)
    app.url_map.converters['regex'] = RegexConverter
    app.config.from_pyfile('config')
    app.config['SQLALCHEMY_DATABASE_URI'] = \
    'sqlite:///' + path.join(basedir, 'data.sqlite')
    app.config['QLALCHEMY_COMMIT_ON_TEARDOWN'] = True

    nav.register_element('top', Navbar(u'Flask入门',
                                   View(u'主页', 'index'),
                                   View(u'关于', 'about'),
                                   View(u'服务', 'services'),
                                   View(u'项目', 'projects'),
                                   ))
    db.init_app(app)
    bootstrap.init_app(app)
    nav.init_app(app)
    init_views(app)
    return app

manager.py


 from werkzeug.utils import secure_filename
from flask.ext.script import Manager
from app import create_app

app = create_app()
manager = Manager(app)


@manager.command
def dev():
    from livereload import Server
    live_server = Server(app.wsgi_app)
    live_server.watch('**/*.*')
    live_server.serve(open_url=True)


@manager.command
def test():
    pass


@manager.command
def deploy():
    pass

if __name__ == '__main__':
    manager.run()

models.py


 # -*- encoding: utf-8 -*-
from . import db

class Role(db.Model):
    __tablename__ = 'roles' 
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String, nullable=True)
    users = db.relationship('User', backref='roles')


class User(db.Model):
    __tablename__ = 'users'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String, nullable=True)
    password = db.Column(db.String, nullable=True)
    role_id = db.Column(db.Integer, db.ForeignKey('roles.id'))

请问有开发经验的朋友来解答我的问题吗?

flask sqlalchemy python2.7

blind 9 years, 5 months ago

有一种可能性:

  1. db __init__.py 定义

  2. models.py 引入了 db

  3. views.py 引入了 models.py

  4. __init__.py 引入 views.py

  5. db 循环引入了

无数的小明 answered 9 years, 5 months ago

Your Answer