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.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

How to delete and recreate a postgres database using a single docker command?

分類Dev

from raw sql to flask-sqlalchemy

分類Dev

Flask-SQLAlchemy: How to change table structure?

分類Dev

How to update Boolean Value in Flask SQLAlchemy

分類Dev

How does one recreate (merged) branch that was deleted from local and remote?

分類Dev

Getting all unique values and the count of their occurences from Flask SQLAlchemy table

分類Dev

flask_sqlalchemy create model from different file

分類Dev

How to call recreate()?

分類Dev

How to mock <ModelClass>.query.filter_by() in Flask-SqlAlchemy

分類Dev

How to hide MYSQL credentials in SQLAlchemy using Flask application

分類Dev

How to create test database with Flask-Testing

分類Dev

Flask-SQLAlchemy:SQLALCHEMY_DATABASE_URIもSQLALCHEMY_BINDSも設定されていません。デフォルトのSQLALCHEMY_DATABASE_URIは「sqlite:///:memory:」です。

分類Dev

How to recreate the any? method in Ruby

分類Dev

How to recreate the progress bar rotation?

分類Dev

When submitting a Flask WTForms, how can fields be left blank without blanking-out those values from the database table?

分類Dev

How to get dict of lists from relationship in sqlalchemy?

分類Dev

How to connect to Postgres database on Docker in Windows 10 using SQLAlchemy?

分類Dev

sqlalchemy store media in the database

分類Dev

Flask-SQLAlchemy association table

分類Dev

flask-sqlalchemy. PhoneNumber type

分類Dev

Flaskの継承-SqlAlchemy

分類Dev

Flask-Admin: how to add the database view to a different site

分類Dev

In Flask-SQLAlchemy: How do I set check_same_thread=False in config.py?

分類Dev

flask-sqlalchemy: how to define the 'comment' model (multi foreign keys referring to the same table)?

分類Dev

How to pass argument from template to view in Flask

分類Dev

How to retreive stack from Database

分類Dev

Accidentally deleted .xcodeproj file how to recreate it?

分類Dev

Flask login (no database)

分類Dev

Flask initialize database with values

Related 関連記事

  1. 1

    How to delete and recreate a postgres database using a single docker command?

  2. 2

    from raw sql to flask-sqlalchemy

  3. 3

    Flask-SQLAlchemy: How to change table structure?

  4. 4

    How to update Boolean Value in Flask SQLAlchemy

  5. 5

    How does one recreate (merged) branch that was deleted from local and remote?

  6. 6

    Getting all unique values and the count of their occurences from Flask SQLAlchemy table

  7. 7

    flask_sqlalchemy create model from different file

  8. 8

    How to call recreate()?

  9. 9

    How to mock <ModelClass>.query.filter_by() in Flask-SqlAlchemy

  10. 10

    How to hide MYSQL credentials in SQLAlchemy using Flask application

  11. 11

    How to create test database with Flask-Testing

  12. 12

    Flask-SQLAlchemy:SQLALCHEMY_DATABASE_URIもSQLALCHEMY_BINDSも設定されていません。デフォルトのSQLALCHEMY_DATABASE_URIは「sqlite:///:memory:」です。

  13. 13

    How to recreate the any? method in Ruby

  14. 14

    How to recreate the progress bar rotation?

  15. 15

    When submitting a Flask WTForms, how can fields be left blank without blanking-out those values from the database table?

  16. 16

    How to get dict of lists from relationship in sqlalchemy?

  17. 17

    How to connect to Postgres database on Docker in Windows 10 using SQLAlchemy?

  18. 18

    sqlalchemy store media in the database

  19. 19

    Flask-SQLAlchemy association table

  20. 20

    flask-sqlalchemy. PhoneNumber type

  21. 21

    Flaskの継承-SqlAlchemy

  22. 22

    Flask-Admin: how to add the database view to a different site

  23. 23

    In Flask-SQLAlchemy: How do I set check_same_thread=False in config.py?

  24. 24

    flask-sqlalchemy: how to define the 'comment' model (multi foreign keys referring to the same table)?

  25. 25

    How to pass argument from template to view in Flask

  26. 26

    How to retreive stack from Database

  27. 27

    Accidentally deleted .xcodeproj file how to recreate it?

  28. 28

    Flask login (no database)

  29. 29

    Flask initialize database with values

ホットタグ

アーカイブ