when trying to create a bank with flask_sqlalchemy Attributeerror: can’t set attribute

Asked

Viewed 59 times

0

Making a Flask app, I had some difficulties with the database, and followed the following steps in VSCODE:

  • I created a virtual environment, installed via Pip flask and other libs
  • I created the first function with the database :
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
db = SQLAlchemy(app)
  • With the app with its first class, I used the terminal to open the database in sql

class Todo(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    content = db.Column(db.String(200), nullable=False)
    date_created = db.Column(db.DateTime, default=datetime.utcnow)

def __repr__(self):
        return '<Task %r>' % self.id 

  • To import the bank class, I used the code :
from yourapplication import db
  • But when trying to create the bank :
db.create_all()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\Domestico\Desktop\pyp\Flask\Flask site\env\lib\site-packages\flask_sqlalchemy\__init__.py", line 1039, in create_all
    self._execute_for_all_tables(app, bind, 'create_all')
  File "C:\Users\Domestico\Desktop\pyp\Flask\Flask site\env\lib\site-packages\flask_sqlalchemy\__init__.py", line 1031, in _execute_for_all_tables
    op(bind=self.get_engine(app, bind), **extra)
  File "C:\Users\Domestico\Desktop\pyp\Flask\Flask site\env\lib\site-packages\flask_sqlalchemy\__init__.py", line 962, in get_engine
    return connector.get_engine()
  File "C:\Users\Domestico\Desktop\pyp\Flask\Flask site\env\lib\site-packages\flask_sqlalchemy\__init__.py", line 555, in get_engine
    options = self.get_options(sa_url, echo)
  File "C:\Users\Domestico\Desktop\pyp\Flask\Flask site\env\lib\site-packages\flask_sqlalchemy\__init__.py", line 570, in get_options
    self._sa.apply_driver_hacks(self._app, sa_url, options)
  File "C:\Users\Domestico\Desktop\pyp\Flask\Flask site\env\lib\site-packages\flask_sqlalchemy\__init__.py", line 914, in apply_driver_hacks
    sa_url.database = os.path.join(app.root_path, sa_url.database)
AttributeError: can't set attribute

1 answer

0

Two points you should understand, the first is that in your app settings SQLALCHEMY_DATABASE_URI should have 4 bars like this : 'sqlite:////test.db', second point in class All there must be an attribute matching the name of the database table, there must be the attribute __tablename__ with the name of the ex table: __tablename__='users'

  • Sorry, I did not inform this because I imagined to be standard, but the space config already exists (although I put in the final directory 'sqlite://test.db' to globalize access) but in the class there really is no final attribute, but I imagine that the error does not signal this

Browser other questions tagged

You are not signed in. Login or sign up in order to post.