Hello I have the following folder structure
.
├── api
│ ├── users
│ │ ├── __init__.py
│ │ ├── model.py
│ │ └── routes.py
│ ├── |__init__.py
├── __init__.py
├── requirements.txt
└── server.py
In api.__init__.py I have the following code:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from api import users
from flask_restful import Api
db = SQLAlchemy()
def create_app(env):
app = Flask(__name__)
db.init_app(app)
api = Api(app)
users.register_routes(api)
return app
and in users/model.py I have the following code:
from sqlalchemy import Column, Integer, String
from .. import db
class User(db.Model):
__tablename__ = 'users'
...
And in server.py I have the following code:
import os
from api import create_app
app = create_app(os.getenv("ENV"))
if __name__ == "__main__":
app.run(port=os.getenv("PORT"), debug=os.getenv("DEBUG")=='True')
The problem is that when I execute server.py I have the following error:
AttributeError: module 'api.db' has no attribute 'Model'
How can I solve this error?
Thanks
dbis not overridden insideapi/__init__.py? try to confirm it with a quickprint(db)right afterfrom .. import db. Attaching the full exception will be helpful too.<SQLAlchemy engine=None>so my guess it's overridden with something else. Probably you have aapi/db.py?