Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello there! I'm trying to make a website with a database, but when I commit an user's account to the DB, I get a sqlalchemy.exc.OperationalError

I'm using flask to make the website.

Is there anything I can do to fix this?


Here is the code causing the error:
new_user = User(userName=username, password=gph(password,method="sha256"))
db.session.add(new_user)
db.session.commit()


I'm using python version 3.10.0 if that helps.

What I have tried:

I'm new to databases and things like that, so I don't know how I could be able to fix this.

If you need the rest of my code I can send it thru pastebin
Posted
Updated 8-Mar-22 21:34pm
v2
Comments
bingusCoder 6-Mar-22 8:59am    
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: user
[SQL: INSERT INTO user ("userName", password) VALUES (?, ?)]
[parameters: ('bingusCoder', 'sha256$Mq86GzkNlqr3gN7M$1fd1b3bd7be943894035abfdb882ada80367a08908302c6119a8bfceb50db881')]
(Background on this error at: https://sqlalche.me/e/14/e3q8)
Maciej Los 6-Mar-22 9:03am    
An error message is quite obvious: "no such table: user
Check if table user exists.
bingusCoder 6-Mar-22 10:06am    
It does exist. Here's my models file: https://pastebin.com/xEGB3q9X
CHill60 7-Mar-22 6:23am    
That website is blocked for many people at work.
You think the table exists, but it clearly does not. Are you using the right database or the right schema. You're trying to share your models file but have you actually generated the database from the model?

1 solution

As per my comment, you will need to generate the database from your model using the create_all method - see API — Flask-SQLAlchemy Documentation (2.x)[^]

Also this reference might help : Quickstart — Flask-SQLAlchemy Documentation (2.x)[^]
 
Share this answer
 
Comments
bingusCoder 9-Mar-22 19:17pm    
It's still not working. Here's the code for my create_app function:
def create_app():
app = Flask(__name__)
app.config['SECRET_KEY'] = 'confidential'
app.config["SQLALCHEMY_DATABASE_URI"] = f"sqlite:///{DB_NAME}"
print("Initializing app")
db.init_app(app)
print("App initialized")

print("Importing blueprints...")
from .views import views
from .auth import auth

app.register_blueprint(views, url_prefix="/")
app.register_blueprint(auth, url_prefix="/")
print("Registered Blueprints")
print("Importing database")
from .models import user, Reservation

print("Creating database")
create_database(app)
db.create_all(app=app)

return(app)

Here's the code for my create_database function:
def create_database(app):
print("Checking if DB exists")
if not path.exists("website/" + DB_NAME):
db.create_all(app=app)
print("DB did not exist, has been created right now.")
else:
print("DB exists, nothing has been done.")
Thanks for helping!
CHill60 10-Mar-22 4:28am    
When you say "not working" are you getting any error messages? Do the solutions on this post help python - SQLAlchemy create_all() does not create tables[^]
bingusCoder 10-Mar-22 13:16pm    
Nope, doesn't help. It says that there's no such table named user even though I have created a User class and imported it, AND have done create_all()
CHill60 11-Mar-22 19:14pm    
It's all about the order you do stuff. Model..defined..can now generate database. You're either missing a step or doing a step out of order
bingusCoder 11-Mar-22 19:49pm    
Here's the code for where I create the new user.
new_user = user(userName=username, password=gph(password,method="sha256"))
db.session.add(new_user)
db.session.commit()

Is anything wrong?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900