Created
October 23, 2012 19:49
-
-
Save mrjoes/3941123 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2012-10-23 22:48:53,448 INFO sqlalchemy.engine.base.Engine INSERT INTO user (username, email) VALUES (?, ?) | |
2012-10-23 22:48:53,450 INFO sqlalchemy.engine.base.Engine ('test', 'email') | |
2012-10-23 22:48:53,450 INFO sqlalchemy.engine.base.Engine INSERT INTO user_info ("key", value, user_id) VALUES (?, ?, ?) | |
2012-10-23 22:48:53,450 INFO sqlalchemy.engine.base.Engine ('a', 'b', 1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask | |
from flask.ext.sqlalchemy import SQLAlchemy | |
# Create application | |
app = Flask(__name__) | |
# Create dummy secrey key so we can use sessions | |
app.config['SECRET_KEY'] = '123456790' | |
# Create in-memory database | |
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.sqlite' | |
app.config['SQLALCHEMY_ECHO'] = True | |
db = SQLAlchemy(app) | |
# Create models | |
class User(db.Model): | |
id = db.Column(db.Integer, primary_key=True) | |
username = db.Column(db.String(80), unique=True) | |
email = db.Column(db.String(120), unique=True) | |
# Required for administrative interface | |
def __unicode__(self): | |
return self.username | |
class UserInfo(db.Model): | |
id = db.Column(db.Integer, primary_key=True) | |
key = db.Column(db.String(64), nullable=False) | |
value = db.Column(db.String(64)) | |
user_id = db.Column(db.Integer(), db.ForeignKey(User.id)) | |
user = db.relationship(User, backref='info') | |
def __unicode__(self): | |
return '%s - %s' % (self.key, self.value) | |
if __name__ == '__main__': | |
db.create_all() | |
user = User() | |
user.username = 'test' | |
user.email = 'email' | |
db.session.add(user) | |
user_info = UserInfo() | |
user_info.key = 'a' | |
user_info.value = 'b' | |
user_info.user = user | |
db.session.add(user_info) | |
db.session.commit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment