Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why SQLalchemy create_all() can be reused?

I have very little background knowledge about database and SQLalchemy/sqlite3, but my experiment with these tells me that create_all() can be used almost like "initialize the database if it already exists".

Here is the "form submission" part of flask webapp I wrote:

db = SQLAlchemy(app)
@app.route('/submit', methods=['POST'])
def submit():
    form = UserForm(request.form)
    if request.method == 'POST':
        new_entry = User(form.username.data,
                         form.password.data)
        db.create_all()
        add_entry(new_entry)

The db.create_all() should create a local .db file. If I already have that file, and I submit new data to the form, what happened is that the new_entry is appended to the database table instead of override that .db file. Can anyone confirm that this result is expected? And what is the better way to handle database initialization if this is poorly implemented?

like image 418
return 0 Avatar asked Jan 15 '16 08:01

return 0


People also ask

What is DB Create_all?

Sqlalchemy create_all method is used to create a new table into the database. This method will first check whether the table exists in the database or not if suppose it has found an existing table it will not create any table.

What is the difference between SQLAlchemy and flask SQLAlchemy?

One of which is that Flask-SQLAlchemy has its own API. This adds complexity by having its different methods for ORM queries and models separate from the SQLAlchemy API. Another disadvantage is that Flask-SQLAlchemy makes using the database outside of a Flask context difficult.

What does SQLAlchemy app do?

SQLAlchemy is an SQL toolkit that provides efficient and high-performing database access for relational databases. It provides ways to interact with several database engines such as SQLite, MySQL, and PostgreSQL. It gives you access to the database's SQL functionalities.

What is flask SQLAlchemy?

Flask SQLAlchemy is an ORM tool which establishes the relationship between the objects and the tables of the relational databases.


1 Answers

create_all indeed creates only tables that do not exist. It is not intended for migrations but to create the initial structure in an empty database.

You want alembic for migrations, possibly via Flask-Migrate


Besides that, having any database structure related code in the web-facing part of your Flask app is questionable. Usually you do such things in a command-line tool (via Flask-Script or click), e.g. python manage.py create_db.

I disagree with your opinion of "poorly implemented". It's rather implemented in a way that stops you from doing the wrong (and possibly dangerous) thing. Imagine you rename a column. A ""smart"" create_all would DROP the old column (it's gone after all) and create a new one - it's impossibly for any tool to know that you renamed it instead of removing and adding one.

like image 98
ThiefMaster Avatar answered Sep 18 '22 16:09

ThiefMaster