未解析的属性“列"在"SQLAlchemy"类中 [英] unresolved attribute "Column" in class "SQLAlchemy"

查看:601
本文介绍了未解析的属性“列"在"SQLAlchemy"类中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用pycharm 5.0和python3.5.并且我通过pycharm的内置函数(setting-project-project解释器-"+")下载了所有的liarbry.其他库看起来不错,但是烧瓶发生了一些问题-SQLAlchemy.

i use pycharm 5.0 and python3.5.And i download all the liarbry by the build-in function of pycharm(setting-project-project interpreter-"+").other libraries appear well,but some problems happens to flask-SQLAlchemy.

我成功导入了flask-SQLAlchemy.但是,pycharm提醒我类'SQLAlchemy'中未解决的属性引用'Column'".类'SQLAlchemy'中未解决的属性引用'relationship'",依此类推.

i import flask-SQLAlchemy successfully.however,pycharm remind me that "unresolved attribute reference 'Column' in class'SQLAlchemy'"."unresolved attribute reference 'relationship' in class 'SQLAlchemy'" and so on.

我尝试了一些方法,但是没有用.例如:1.重新启动2.删除并重新下载3.刷新缓存.

I have try some ways ,but they didn't work.for example:1.restart 2.remove and redownload 3.refresh the cache.which mention in PyCharm shows unresolved references error for valid code

代码:

from flask import Flask, redirect, render_template, session, url_for, flash
from flask_sqlalchemy import SQLAlchemy
from flask_bootstrap import Bootstrap
from flask_wtf import Form
from wtforms import StringField, SubmitField
import os
from wtforms.validators import data_required


basedir = os.path.abspath(os.path.dirname(__file__))


app = Flask(__name__)
app.config['SECRET_KEY'] = 'hard to guess string'
app.config['SQLALCHEMY_DATABASE_URI'] =\
    'sqlite:///' + os.path.join(basedir, 'data.sqlite')
app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True


bootstrap = Bootstrap(app)

db = SQLAlchemy(app)


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

    def __repr__(self):
        return '<Role %r>' % self.name


class User(db.Model):
    __tablename__ = 'users'
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(64), unique=True, index=True)
    role_id = db.Column(db.Integer, db.ForeignKey('roles.id'))

    def __repr__(self):
        return '<User %r>' % self.username

我该如何解决这个问题?

how can i solve this problem?

推荐答案

flask_sqlalchemy.SQLAlchemy类的构造函数调用

The constructor of the flask_sqlalchemy.SQLAlchemy class calls _include_sqlalchemy, which attaches all attributes from sqlalchemy and sqlalchemy.orm to its instances.

这仅在运行时完成,PyCharm的代码检查未检测到.

This is only done at runtime and not detected by PyCharm's code inspection.

这将要求flask_sqlalchemy使用更标准的方式导入这些属性,例如from sqlalchemy import *.但这会将属性(而不是每个SQLAlchemy实例)导入到flask_sqlalchemy模块中,从而改变了访问属性的方式.

It would require flask_sqlalchemy to use a more standard way of importing those attributes, like from sqlalchemy import *. But this would import the attributes into the flask_sqlalchemy module instead of each instance of SQLAlchemy and thus change the way they're accessed.

我不是Python或SQLAlchemy专家,并且不会判断这是否是好的设计,但是您可以在 https://github.com/mitsuhiko/flask-sqlalchemy 并在那里进行讨论.

I'm not a Python or SQLAlchemy expert and won't judge whether this is good design or not but you could open an issue on https://github.com/mitsuhiko/flask-sqlalchemy and discuss it there.

这篇关于未解析的属性“列"在"SQLAlchemy"类中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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