vendredi 15 janvier 2016

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?

Aucun commentaire:

Enregistrer un commentaire