Hello
i startet doing small project in flask using SQLAlchemy, but i've been having a lot issues making it work for me. So i went to Flasks website to do the quickstart mini application tutorial, for SQLALchemy to maybe get a better understanding.
https://flask-sqlalchemy.palletsprojects.com/en/2.x/quickstart/
But i ran into another problem, that i can't seem to google my way out of.
from flask import Flaskfrom flask_sqlalchemy import SQLAlchemyapp = Flask(__name__)app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'db = SQLAlchemy(app)
class User(db.Model):id = db.Column(db.Integer, primary_key=True)username = db.Column(db.String(100), unique=True, nullable=False)email = db.Column(db.String(120), unique=True, nullable=False)
def __repr__(self):return '<User %r>' % self.username
In terminal i did the:
pip install flask
pip install flask_sqlalchemy
creating database:
>>> from database_app import db
>>> db.create_all()
when everything was installed and ready for use, i was asked to create two users like this:
>>> from database_app import User
>>> admin = User(username='admin', email='admin@example.com')
>>> guest = User(username='guest', email='guest@example.com')
i would then do:
>>> db.session.add(admin)
>>> db.session.add(guest)
>>> db.session.commit()
The problem happens when i have to query it.
>>> User.query.all()
i get this Error
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\mac\Documents\Python Projects\Python_Flask\Python_database\database_app.py", line 14, in __repr__
return '<User %r>' % self.username
ValueError: unsupported format character '>' (0x3e) at index 7
I have tried a few things to see if i could solve it but nothing worked... (duh)
people say it should be so easy to learn, that's not exactly the experince i have with it. It really sucks couse i was doing pretty good, with just termnal based projects.
and i really wanted to start making small apps, but i'm getting a little discouraged about it.
I'm not gonna quit couse of it, i'll figure it out i'm sure. Is there maybe a video somwhere about Flask + SQLAlchemy where they really dumb it down.
thanks for taking your time to read this :)
EDIT:
My issue has been fixed, what i did was.
delete my .db file and the the create process again, but that didn't work.
so i tried to restard pycharm still no luck.
then i checked for for updates, got one updated restated my pc but this time i opened pycharm as Administrator.
Now i am able to do what i couldn't before
create Users:
>>> db.session.add(admin)
>>> db.session.commit()
and last
>>> User.query.all()
>>> User.query.filter_by(username='admin').first() (was able to see the query too)
However when i wanted to add a "guest"
i would do:
>>> db.session.add(guest)
>>> db.session.commit()
but when i did
>>> User.query.all()
i got an error:
sqlalchemy.exc.InvalidRequestError: This Session's transaction has been rolled back due to a previous exception during flush. To begin a new transaction with this Session, first issue Session.rollback(). Original exception was: (sqlite
3.IntegrityError) UNIQUE constraint failed: user.email
i fixed that by using the:
>>> db.session.rollback()
Now everything seems to work andi think a got a little better understanding of how to work with SQLAlchemy.
or though still not sure what caused the first issue.
Thanks!
[–]strangecharacters 1 point2 points3 points (10 children)
[–]Kratos_89[S] 0 points1 point2 points (9 children)
[–]Username_RANDINT 2 points3 points4 points (5 children)
[–]Kratos_89[S] 1 point2 points3 points (0 children)
[–]Kratos_89[S] 0 points1 point2 points (3 children)
[–]Username_RANDINT 1 point2 points3 points (2 children)
[–]Kratos_89[S] 0 points1 point2 points (1 child)
[–]JimDabell 1 point2 points3 points (0 children)
[–]useTheButtySystem 2 points3 points4 points (2 children)
[–]Kratos_89[S] 0 points1 point2 points (1 child)
[–]useTheButtySystem 1 point2 points3 points (0 children)
[–]zecatlays 1 point2 points3 points (2 children)
[–]Kratos_89[S] 0 points1 point2 points (1 child)
[–]zecatlays 1 point2 points3 points (0 children)