V
V
vladibuyanov2021-03-18 17:46:35
Flask
vladibuyanov, 2021-03-18 17:46:35

How to fix error while creating SQLAlchemy table?

Hello!
Unable to create sqllite table. When creating a table in the python console, I use the commands
>>> from main import db
>>> db.create_all() It throws an

error AttributeError: can't set attribute

Here is my application code:

from flask import Flask
from flask_sqlalchemy import SQLAlchemy


app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///tmp/dpd_test.db '
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)


class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True, nullable=False)
    email = db.Column(db.String(120), unique=True, nullable=False)


@app.route('/')
def hello_world():
    return 'Hello, World!'


Thank you very much for your attention

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
nullnull, 2021-03-19
@nullnull

Perhaps the problem is in the path to the db.
Try replacing this

app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///tmp/dpd_test.db '

on this
import os
app.config['SQLALCHEMY_DATABASE_URI'] =  'sqlite:///' + os.path.join( os.path.abspath(os.path.dirname(__file__)), 'mydb.db' )

Here in os.path.join you can after os.path.abspath. separated by commas, specify the folders and file name as you like, of course.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question