'SQLAlchemy'的实例没有'Column'成员



我正在用python和flask创建我的第一个后端,我遇到了这个错误:"SQLAlchemy"的实例没有"Column"成员;。我试图通过以下来自stackoverflow的类似问题来解决这个问题,但它不起作用。我尝试过的解决方案:

  • 安装烧瓶sqlalchemy,

  • 已安装烧瓶,

  • 添加

"python.linting.pylintArgs": [
"--load-plugins", 
"pylint-flask-sqlalchemy",
"pylint-flask"
] 

到我的settings.json文件,

基本上,我遵循了这个线程:';SQLAlchemy';没有';列';委员(无委员(但没有成功。我试图实现的实际上只是复制粘贴这个图图里亚尔:https://www.codementor.io/@dongido/如何构建一个完整的

我已经成功地完成了第五步,那就是创建模型。

有人知道如何解决这个问题的进一步信息吗?

更新:当我运行程序时,我得到:

Traceback (most recent call last):
File "run.py", line 17, in <module>
app = create_app("config")
File "run.py", line 10, in create_app
from Model import db
File "C:UsersMadsenAndroidStudioProjectsJustDoItAppBackendModel.py", line 3, in <module>
from flask_marshmallow import Marshmallow
File "C:UsersMadsenAndroidStudioProjectsJustDoItAppBackendenvlibsite-packagesflask_marshmallow__init__.py", line 24, in <module> import flask_sqlalchemy  # flake8: noqa
File "C:UsersMadsenAndroidStudioProjectsJustDoItAppBackendenvlibsite-packagesflask_sqlalchemy__init__.py", line 39, in <module>
_timer = time.clock
AttributeError: module 'time' has no attribute 'clock'

程序还没有准备好运行。但在我下面的图图里亚尔,这不应该导致一个错误。可能是我连接的数据库有问题吗?我正在从confi.py:连接到我的localhostpostgresql,如下所示

SQLALCHEMY_DATABASE_URI = os.environ.get("postgresql://postgres:password@localhost/BilletApp")

我已经用本地主机密码填充了密码字段。这个数据库没有名为"comments"的表,就像我试图用下面的代码创建的一样:

class Comment(db.Model):
__tablename__ = 'comments'
id = db.Column(db.Integer, primary_key=True)
comment = db.Column(db.String(250), nullable=False)
creation_date = db.Column(db.TIMESTAMP, server_default=db.func.current_timestamp(), nullable=False)
category_id = db.Column(db.Integer, db.ForeignKey('categories.id', ondelete='CASCADE'), nullable=False)
category = db.relationship('Category', backref=db.backref('comments', lazy='dynamic' ))
def __init__(self, comment, category_id):
self.comment = comment
self.category_id = category_id.

这可能是问题所在吗?

编辑一


  • pip-install-pylint
  • pip安装锥形烧瓶
  • pip安装pylint烧瓶sqlalchemy

打开VSCode并运行Ctrl+Shift+p(适用于Windows用户(

Mac 上的shift+command+p

  • 弹出框中的"Python:启用Linting",然后选择"on">

再次在Mac 上运行Ctrl+Shift+p(适用于Windows用户(或Shift+command+p

  • 键入"首选项:打开工作区设置"并选择它以打开文件。

  • 将其添加到文件中:

    "python.linting.pylintEnabled": true,
    "python.linting.enabled": true,
    "python.linting.pylintUseMinimalCheckers": false,
    "python.linting.pylintArgs": [
    "--load-plugins",
    "pylint_flask_sqlalchemy, pylint_flask",
    "--init-hook"
    ]
    

好的,所以Gord Thompson可能是正确的,但也可能取决于代码本身。现在我无法打开代码教程,因为我的工作站,如果可以的话,我稍后会尝试(除非下面的内容可以解决问题(

现在一步到位

通过观察您收到的错误,"模块"时间没有"时钟"属性:

从操作系统回答此处:--->贡献者:Abgus-Tay

函数time.clock((自Python 3.3以来一直被弃用,现在已经被删除:根据您的需求,使用time.perf_counter((或time.process_time((来获得定义良好的行为。

两个

正如我所说,我现在没有看到你正在学习的代码教程,但它可能已经过时了

从操作系统回答此处--->贡献者:Simone Bronzini

db.Column(db.TIMESTAMP, default=datetime.utcnow,server_default=text('0'), nullable=False,)

或者需要编辑

created_time   = Column(TIMESTAMP, nullable=False, server_default=func.now())
updated_time   = Column(TIMESTAMP, nullable=False, server_default=text('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'))

或者不使用TIMESTAMP,为什么不使用DateTime?

created_date = Column(DateTime, default=datetime.datetime.utcnow)

我找到的解决方案是将我的IDE更改为PyCharm,而不是vs代码。问题似乎是pylint没有跟上最新版本的python,也无法理解这个错误会在运行时在虚拟环境中处理,因此erorr:";"SQLAlchemy"的实例没有"Column"成员";,会被突出显示,如上所述,我需要忽略设置中的错误来消除恼人的红色下划线。在PyCharm中,它似乎知道它将由虚拟环境处理,因此IDE不会标记为错误。

您是否在';SQLAlchemy';没有';列';议员(无议员(我认为它已经清楚地解释了这个问题。事实上,这是pylint的一个问题,你可以切换到另一个linter来解决这个问题,比如flake8、mypy、pydocstyle等等

最新更新