Skip to content

Instantly share code, notes, and snippets.

Created September 25, 2014 16:19
Show Gist options
  • Save anonymous/795a8db6a2fa50f68394 to your computer and use it in GitHub Desktop.
Save anonymous/795a8db6a2fa50f68394 to your computer and use it in GitHub Desktop.
Delete/Create single table.
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite://' ## Just keep it in memory.
db = SQLAlchemy(app)
class User(db.Model):
__tablename__ = 'user'
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True)
def __init__(self, username):
self.username = username
def __repr__(self):
return '<User %r>' % self.username
class Another(db.Model):
__tablename__ = 'another'
id = db.Column(db.Integer, primary_key=True)
field = db.Column(db.String())
def __init__(self, field):
self.field = field
def __repr__(self):
return '<Another %r>' % self.field
if __name__ == '__main__':
with app.app_context():
db.create_all()
db.session.add(Another(field='Foo'))
db.session.add(User(username='Bob'))
db.session.commit()
print User.query.first()
# <User u'Bob'>
print Another.query.first()
# <Another u'Foo'>
User.__table__.drop(db.engine)
User.__table__.create(db.engine)
print User.query.first()
# None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment