How to recreate database in SQLAlchemy from Flask?

baileyhaldwin

I'm using SQLAlchemy ORM framework from a Flask project. I wanted to add another column to one of my models. After I did, I used db.session.drop_all() to drop all current entries in the database, and then I tried recreating a new instance with the new field.

Whenever I tried I get this error

sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no 
such table: user
[SQL: SELECT user.id AS user_id, user.username AS user_username, user.email AS user_email, user.password AS user_password, user.image_file AS user_image_file 
FROM user]

I think I might need to reconstruct the database in some way, but I'm not sure how. I looked into the documentation, but could not find anything useful.

__init__.py

app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///site.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

models.py

class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(20), unique=True, nullable=False)
    email = db.Column(db.String(40), unique=True, nullable=False) #newly added field
    password = db.Column(db.String(60), nullable=False)
    image_file = db.Column(db.String(20), nullable=False, default='default.jpg')
    messages = db.relationship('Message', backref='user', lazy=True)
kemis

When you used db.drop_all() you dropped all tables so now you can't insert data because there is no table. You need create the table again with db.create_all() as @SuperShoot mentioned.

You should use something for migrations like alembic or flask-sqlalchemy. This way you could add new column to your ORM. Run flask db migrate -m 'new column', this will detect changes. Then run flask db upgrade head to apply those changes to your database.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to receive link from database in flask sqlalchemy

From Dev

How to get (Retrieve) data from SQlite database in Flask using sqlalchemy

From Dev

How to select only some of the columns from the database with Flask-SQLAlchemy?

From Dev

How to get data from flask-sqlalchemy database

From Dev

How to delete a record from a database in flask-SQLAlchemy?

From Dev

SQLAlchemy: how to recreate the table

From Dev

How to access an external MySQL database from within a PythonAnywhere Flask Webapp (via Pandas&sqlalchemy): Receiving sqlalchemy.exc.OperationalError

From Dev

Creating database with SQLAlchemy in Flask

From Dev

Creating a database in flask sqlalchemy

From

How to delete and recreate from scratch an existing EF Code First database

From Dev

How to return Json from Flask-SQLAlchemy

From Dev

change database migrations from sqlalchemy-migrate to Flask-Migrate

From Dev

Python Flask not deleting correct id from SQLAlchemy database

From Dev

Get choices from a DataBase query in wtforms and flask-sqlalchemy

From Dev

How to correctly setup Flask + uWSGI + SQLAlchemy to avoid database connection issues

From Dev

How do I call a database function using SQLAlchemy in Flask?

From Dev

How to add flask-RBAC models to existing SQLAlchemy database

From Dev

How to properly run consecutive tests querying a Flask-SQLAlchemy database?

From Dev

How to store API requests in database using Flask and SQLAlchemy

From Dev

How not to send null data to a database created with SQLalchemy in flask application?

From Dev

Flask SQLalchemy, password is not stored in database

From Dev

How to automaticaly insert a row in flask-sqlalchemy while creating the database in flask

From Dev

How to stream CSV from Flask via sqlalchemy query?

From Dev

How can print input from flask-WTForm to html with SQLALCHEMY

From Dev

How to access the content of a single column from a flask-sqlalchemy model?

From Dev

How to get all books from an author using flask SQLAlchemy?

From Dev

How to display the values in a list from the database in flask?

From Dev

How to recreate a database on an Android Application Update?

From Dev

How to load a pandas dataframe from ORM SqlAlchemy from an existing database?

Related Related

  1. 1

    how to receive link from database in flask sqlalchemy

  2. 2

    How to get (Retrieve) data from SQlite database in Flask using sqlalchemy

  3. 3

    How to select only some of the columns from the database with Flask-SQLAlchemy?

  4. 4

    How to get data from flask-sqlalchemy database

  5. 5

    How to delete a record from a database in flask-SQLAlchemy?

  6. 6

    SQLAlchemy: how to recreate the table

  7. 7

    How to access an external MySQL database from within a PythonAnywhere Flask Webapp (via Pandas&sqlalchemy): Receiving sqlalchemy.exc.OperationalError

  8. 8

    Creating database with SQLAlchemy in Flask

  9. 9

    Creating a database in flask sqlalchemy

  10. 10

    How to delete and recreate from scratch an existing EF Code First database

  11. 11

    How to return Json from Flask-SQLAlchemy

  12. 12

    change database migrations from sqlalchemy-migrate to Flask-Migrate

  13. 13

    Python Flask not deleting correct id from SQLAlchemy database

  14. 14

    Get choices from a DataBase query in wtforms and flask-sqlalchemy

  15. 15

    How to correctly setup Flask + uWSGI + SQLAlchemy to avoid database connection issues

  16. 16

    How do I call a database function using SQLAlchemy in Flask?

  17. 17

    How to add flask-RBAC models to existing SQLAlchemy database

  18. 18

    How to properly run consecutive tests querying a Flask-SQLAlchemy database?

  19. 19

    How to store API requests in database using Flask and SQLAlchemy

  20. 20

    How not to send null data to a database created with SQLalchemy in flask application?

  21. 21

    Flask SQLalchemy, password is not stored in database

  22. 22

    How to automaticaly insert a row in flask-sqlalchemy while creating the database in flask

  23. 23

    How to stream CSV from Flask via sqlalchemy query?

  24. 24

    How can print input from flask-WTForm to html with SQLALCHEMY

  25. 25

    How to access the content of a single column from a flask-sqlalchemy model?

  26. 26

    How to get all books from an author using flask SQLAlchemy?

  27. 27

    How to display the values in a list from the database in flask?

  28. 28

    How to recreate a database on an Android Application Update?

  29. 29

    How to load a pandas dataframe from ORM SqlAlchemy from an existing database?

HotTag

Archive