1

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?

Madsen
  • 21
  • 1
  • 6
  • is the error coming when you run the program or just on the vscode? Cause if it's on the vscode and not while actually running the program, then it might be some vscode settings issue. – jen Aug 14 '20 at 10:26
  • Thank you for the reply. I will add the result from running the program below as an answer(?), since it is too long for a comment. – Madsen Aug 14 '20 at 10:46
  • all right, i will add it to the original question instead, since i believe this is the correct way to do it? – Madsen Aug 14 '20 at 10:52
  • @Madsen - The `requirements.txt` in the tutorial specifies version 2.3.2 of Flask-SQLAlchemy, which is rather old now. Try upgrading to the latest version (currently 2.4.4). – Gord Thompson Aug 14 '20 at 11:43
  • Thank you for the reply. Unfortunatly it didn't solve the issue. I used the opportunity to update all of the fields from the requirements.txt to the newest releases, without any errors, but without result. I also reloaded the window as suggested in another post after changes. – Madsen Aug 14 '20 at 12:06
  • I post an answer, even if did not solve the issue I do believe it is a close one. As you can see from my answer it may be because the tutorial you are watching is outdated. At the moment I can't follow the URL because of my network I am at. – Federico Baù Aug 14 '20 at 12:16
  • Actually I never seen the db.TIMESTAMP,I only used datetime.datetime.utcnow. (which you will need to import datatime. From datetime import Datetime. Anyway what does timestamp do? – Federico Baù Aug 14 '20 at 12:17
  • To my knowledge TIMESTAMP should very simply return the date and time of executing, so when i execute this, the TIMESTAMP attribute would be filled with today and the time of execution. – Madsen Aug 14 '20 at 12:47
  • `time.clock` was removed in python3.8 - see https://docs.python.org/3/whatsnew/3.8.html#api-and-feature-removals – snakecharmerb Aug 14 '20 at 13:47
  • Thank you for the reply. I have changed the code to: creation_date = db.Column(db.DateTime, default=datetime.datetime.utcnow), and recvieves this error: AttributeError: type object 'datetime.datetime' has no attribute 'datetime'. I have imported datetime like this: from datetime import datetime – Madsen Aug 14 '20 at 13:52
  • Yes I think now the issue is in VS code only – Federico Baù Aug 14 '20 at 13:54
  • did you add "python.linting.pylintArgs": where? in a folder called .vscode and in a file called settings.json ? – Federico Baù Aug 14 '20 at 14:24
  • ". I have imported datetime like this: `from datetime import datetime`" -> just do `import datetime` – snakecharmerb Aug 14 '20 at 14:27
  • Federico: Yes i added python.linting.pylintArgs to a file settings.json in the folder .vscode. snakecharmerb: i tried this aswell, with the same error. So now i just deleted this attribute since it is not important to the purpose of the excerzise for me, whether or not the creation date is saved. This was just a copy from the tuturial i am following. – Madsen Aug 14 '20 at 14:31
  • Does this answer your question? [Instance of 'SQLAlchemy' has no 'Column' member (no-member)](https://stackoverflow.com/questions/53975234/instance-of-sqlalchemy-has-no-column-member-no-member) – Ruben Helsloot Aug 18 '20 at 17:53

3 Answers3

2

EDIT ONE


  • pip install pylint
  • pip install pylint-flask
  • pip install pylint-flask-sqlalchemy

Open VSCode and run Ctrl+Shift+P (for Windows Users)

shift+command+P on Mac

  • ‘Python: Enable Linting’ on the box that pops up and then select ‘ on ‘

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

One

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.

Two

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)
Federico Baù
  • 6,013
  • 5
  • 30
  • 38
  • Thank you for the reply. I tried to import datetime, and then use the: db.Column(db.TIMESTAMP, default=datetime.utcnow,server_default=text('0'), nullable=False,) which gives the following error when i run the program: AttributeError: module 'datetime' has no attribute 'utcnow' i also tried the last version you gave, with Datetime, default=datetime.datetime.utcnow, to which i get: Module 'datetime' has no 'utcnow' member, and also (wierd) Instance of 'SQLAlchemy' has no 'TIMESTAMP' member in addition it doesn't change the error of: Instance of 'SQLAlchemy' has no 'Column' member – Madsen Aug 14 '20 at 12:44
  • This get's even weirder.. Ok I see that you are running in VS code. If you run the app in a terminal does it give the same issue? You need to see where the issue is, if you dont get error then is more VS code related. If you still get an error than we now is somewhere in the code – Federico Baù Aug 14 '20 at 12:48
  • oh, i didn't import i correctly, i used capital D in from datetime import Datetime. When i type "from datetime import datetime" and run the program i get the following: "AttributeError: type object 'datetime.datetime' has no attribute 'datetime'" – Madsen Aug 14 '20 at 12:54
  • Ok try to do this, 1. pip install pylint-flask-sqlalchemy 2.Add this to VScosde json setting file excatly as it is : "python.linting.pylintArgs": ["--load-plugins", "pylint_flask", "pylint_flask_sqlalchemy"] the order is important – Federico Baù Aug 14 '20 at 12:54
  • These steps have been followed from the other post about this issue. In the other thread though, it states that the order should be ["--load-plugins", "pylint_flask_sqlalchemy", "pylint_flask"], but i tried to change it into your suggestion, but unfortunatly it didn't correct the error. – Madsen Aug 14 '20 at 13:01
  • One question, if you run it in a terminal (so no VScode) your code works right? – Federico Baù Aug 14 '20 at 13:03
  • This is actually the first thing i ever do in python, so i am not sure about the succes criteria of running this program. But when i run it from terminal i recieve this error: "AttributeError: type object 'datetime.datetime' has no attribute 'datetime'" – Madsen Aug 14 '20 at 13:06
  • OK but in the terminal I mean a totally separate Terminal (not the one integrated in VSCode example the normal windows CMD). Look try with this one : "python.linting.pylintArgs": ["--load-plugins", "pylint-flask"] This is the one I have in my vscode. I noticed Now that is the same one.. – Federico Baù Aug 14 '20 at 13:07
  • Yes i am running it from windows CMD. where i cd'ed into the folder, and run the code with "python run.py". I also tried to remove the "pylint-flask-sqlalchemy" from the settings.json, but that didn't change anything either. – Madsen Aug 14 '20 at 13:11
  • Thank you very much for the effort. settings.json: [ { "python.pythonPath": "env\\Scripts\\python.exe" }, { "python.linting.pylintEnabled": true, }, { "python.linting.enabled": true, }, { "python.linting.pylintUseMinimalCheckers": false, }, { "python.linting.pylintArgs": [ "--load-plugins", "pylint-flask-sqlalchemy", "pylint-flask", "--init-hook", "import sys; sys.path.insert(0, '/path/to/root/of/current/project')" ] } ] – Madsen Aug 14 '20 at 13:47
  • When i select enable linting: on - it gives me an error: Command 'Python: Enable Linting' resulted in an error (Can not add index to parent of type array). I googled this error, but the results is not about this same issue. – Madsen Aug 14 '20 at 13:48
  • Might be. Now i tried to just remove the attribute "creation_date", and now the code runs in CMD.Should i just ignore the red lines under every "db." in my code, like: id = db.Column(db.Integer, primary_key=True), saying "Instance of 'SQLAlchemy' has no 'Column' member (pylint(no-member))"? – Madsen Aug 14 '20 at 14:07
  • Yes regarding the red undreline, this means is 100% Vscode behaviour. I try to compare with my VScode setting. Means that the settings are wrong. – Federico Baù Aug 14 '20 at 14:15
  • Okay, if you find a setting that sticks out, and could be the reason for the red underline, i would very much appreciate it. if you would comment here with the settings i have to change. I have also googled it, but i cant really find anything on it. – Madsen Aug 14 '20 at 14:32
  • Look if you still see the red underline means that whatever configuration for the pylint is wrong or not working plus it gives you that errors. When you see the red underline gone is because pylint is active. Since I don't know what else to suggest to you, try to install from zero VScode and start a new project with new envirorment. if still give you issue then try to use another IDLE for the moment – Federico Baù Aug 14 '20 at 14:43
  • 1
    Okay. Thank you very much for the effort, much appteciated! – Madsen Aug 14 '20 at 14:46
  • Had you check the answer of 'np8' in the https://stackoverflow.com/questions/53975234/instance-of-sqlalchemy-has-no-column-member-no-member? – Steven-MSFT Aug 18 '20 at 07:44
1

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.

Madsen
  • 21
  • 1
  • 6
  • Excatly as I suggested some time ago in one of my comment, issue is with vscode. I would suggest to leave it for a while and practice with Pycharm, then if you possibility use VScode in a new fresh device and see how it goes. its weird because works for me, plus without the ' "pylint-flask-sqlalchemy". Any nice that you update your question. – Federico Baù Aug 27 '20 at 17:10
  • Yes, i am sorry for not just taking your word for it, haha. Thank you for help, and I can say that your suggestion was the one that lead me to try a different IDE. – Madsen Aug 28 '20 at 22:23
0

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.

Steven-MSFT
  • 7,438
  • 1
  • 5
  • 13
  • Thank you for the reply. Yes i have seen the suggested solution, but shifting to flake8 or one of the other Will, as far as I have seen, not fix the issue, but rather just ignore it. And I am not very fond of having code with errors that Are just being ignored. I believe that something Odd May have happened at some point of My coding, so i AM currently building the projekt form scratch, and Will see if the error reoccurs. – Madsen Aug 20 '20 at 09:22
  • follow up: you are absolutely right it is a problem with pylint, but for some reason i was not able to change into flake8, or just drop pylint, for some reason., I kept getting the error "cannot add index to parent array", so i just completely dropped working with vs code. – Madsen Aug 27 '20 at 07:25