Python Flask Tutorial: Full-Featured Web App Part 4 – Database with Flask-SQLAlchemy




In this Python Flask Tutorial, we will be learning how to create a database using Flask-SQLAlchemy. SQLAlchemy is a great tool for working with databases …

Original source


24 responses to “Python Flask Tutorial: Full-Featured Web App Part 4 – Database with Flask-SQLAlchemy”

  1. i have a problem , when i try to commit in the Post database. it shows

    "SQLite DateTime type only accepts Python "
    sqlalchemy.exc.StatementError: (builtins.TypeError) SQLite DateTime type only accepts Python datetime and date objects as input.

    i wonder what is the problem ? i followed the instructions but got that though..

  2. Hey Corey,
    While creating users from cmdline i got an error as "sqlalchemy.exc.InvalidRequestError: One or more mappers failed to initialize – can't proceed with initialization of other mappers. Triggering mapper: 'mapped class User->user'. Original exception was: Could not determine join condition between parent/child tables on relationship User.posts – there are no foreign keys linking these tables. Ensure that referencing columns are associated with a ForeignKey or ForeignKeyConstraint, or specify a 'primaryjoin' expression."

    Can you please help me out

  3. Stupid question…but,
    What is the purpose of the f in the return of the _repr_ function for the db stuff?
    _repr_ (self)
    return f"User({'self.user'})

    when i run this I get a syntax error
    on the return in the virtual enviroment

  4. First of all thank you for great tutorials. I have a question. for Example, i have to save a passport id to database, what type of column would you use for that? These numbers do not have decimal values and there should be max input of 11

  5. Note: When using Postgres as Production DB, SQLAlchemy creates a table name 'user' from Class User. 'user' is a reserved table in postgres and is used for db admin users. Using pgAdmin I could not see that my table existed. My db worked but I couldn't see the table in pgAdmin sidebar. I could only see the reserved 'user' table. I had to use SELECT * FROM "user"; (double quotes) to get my data. I changed Class 'User' to 'Users' to avoid later confusion. Not sure if this is similar behavior in other dbs. Hope it saves someone a day of head scratching!

  6. Hi Corey, I'm finding it difficult to setup flask-migrate, is this something you cover anywhere, I will keep trying myself, I'm using pipenv so sometimes I get confused about commands I should be running. I'm probably doing something stupid so any pointers from anyone would be great, thanks.

  7. dear Corey: it would be wonderful if you can release a video about Flask-Pymongo or Flask-MongoDB in which you can set up your own database in Mongodb that is basically a NoSQL database. many thanks in advance.

  8. Really Great Work you do!! Now i dont mind paying for this series on udemy or any other kind. I can now ensure that only true and really contents will come from +Corey Schafer

  9. Hi Corey,
    Your videos are very helpful!
    What is the best way to learn about converting SQL queries to SQLAlchemy? I'm currently struggling with joining three tables. It's easy enough to do in SQL however I haven't yet figured out how to convert this into SQLAlchemy. Examples at stackoverflow.com and SQLAlchemy.org haven't helped.
    Would you mind providing an example of a SQLAlchemy join?

Leave a Reply