Reputation: 21
I am creating my first backend with python and flask, and i have run into this error: "Instance of 'SQLAlchemy' has no 'Column' member". I tried to solve the issue by following similar questions here from stackoverflow, but it doesn't work. Solutions i have tried:
installed flask-sqlalchemy,
installed flask,
added
"python.linting.pylintArgs": [
"--load-plugins",
"pylint-flask-sqlalchemy",
"pylint-flask"
]
to my settings.json file,
basicly i followed this thread: Instance of 'SQLAlchemy' has no 'Column' member (no-member) but without succes. What i am trying to achieve is actually just to copy-paste this tuturial: https://www.codementor.io/@dongido/how-to-build-restful-apis-with-python-and-flask-12qto530jd
And i have gotten succesfully through until step number 5, which is creating the model.
Does anyone have further information on how to solve this issue?
UPDATE: When i run the program i get:
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:\Users\Madsen\AndroidStudioProjects\JustDoItApp\Backend\Model.py", line 3, in <module>
from flask_marshmallow import Marshmallow
File "C:\Users\Madsen\AndroidStudioProjects\JustDoItApp\Backend\env\lib\site-packages\flask_marshmallow\__init__.py", line 24, in <module> import flask_sqlalchemy # flake8: noqa
File "C:\Users\Madsen\AndroidStudioProjects\JustDoItApp\Backend\env\lib\site-packages\flask_sqlalchemy\__init__.py", line 39, in <module>
_timer = time.clock
AttributeError: module 'time' has no attribute 'clock'
The program isn't ready for running though. But in the tuturial i am following, this shouldn't be causing an error. Can it be that the database i am connecting to is the problem? I am connecting to my localhost postgresql like this from confi.py:
SQLALCHEMY_DATABASE_URI = os.environ.get("postgresql://postgres:password@localhost/BilletApp")
and i have filled the password field with my localhost password. This database doesn't have table called 'comments', like what i am trying to create here with this code:
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.
Could this be the issue?
Upvotes: 1
Views: 3229
Reputation: 21
The solution i found was to change my IDE to PyCharm instead of vs code. The issue seems to be that pylint isn't up to date with newest version of python, and also wasn't able to understand that this error would be handled in the virtual environment at runtime, and therefore the erorr: "Instance of 'SQLAlchemy' has no 'Column' member", would just be highlighted, and as mentioned above, i would need to ignore the error in the settings to get rid of the annoying red underline. In PyCharm it seems to know that it will be handled by virtual environment, and the IDE therefore doesn't mark as error.
Upvotes: 1
Reputation: 8411
Had you checked the answer by 'np8' in the Instance of 'SQLAlchemy' has no 'Column' member (no-member) ? I think it had explained the problem clearly. And in fact, it's a problem with pylint, you can just switch to another linter to solve this problem such as flake8, mypy, pydocstyle and so on.
Upvotes: 0
Reputation: 7665
EDIT ONE
Open VSCode and run Ctrl+Shift+P (for Windows Users)
shift+command+P on Mac
Again run Ctrl+Shift+P (for Windows Users) or shift+command+P on Mac
Type ‘Preferences: Open Workspace Settings’ and select it to open the file.
Add this to the file:
"python.linting.pylintEnabled": true,
"python.linting.enabled": true,
"python.linting.pylintUseMinimalCheckers": false,
"python.linting.pylintArgs": [
"--load-plugins",
"pylint_flask_sqlalchemy, pylint_flask",
"--init-hook"
]
OK so what Gord Thompson may be correct, but also it may lay on the code its self. Right now I can't open The Code tutorial because of my work station, I will try if I can later (unless what follows will fix the issue)
Now One step at the time
by watching at the error you received 'module 'time' has no attribute 'clock'':
From OS AnswerHere: ---> Contributor: Abgus Tay
The function time.clock() has been removed, after having been deprecated since Python 3.3: use time.perf_counter() or time.process_time() instead, depending on your requirements, to have well-defined behavior.
As I said I don't see the code tutorial your are takinf from at the moment,but it may be dated
From OS Answer Here ---> Contributor: Simone Bronzini
db.Column(db.TIMESTAMP, default=datetime.utcnow,server_default=text('0'), nullable=False,)
Or Need to edit
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'))
or instead ot TIMESTAMP why don't use DateTime instead?
created_date = Column(DateTime, default=datetime.datetime.utcnow)
Upvotes: 2