Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ORM-like to associate a User with a Post. Also, by enforcing the NOT NULL ability of the foreign key column, invalid numbers cannot be added.
- from flask import Flask
- from flask_sqlalchemy import SQLAlchemy
- from datetime import datetime
- app = Flask(__name__)
- app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
- app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
- db = SQLAlchemy(app)
- class User(db.Model):
- id = db.Column(db.Integer, primary_key=True)
- username = db.Column(db.String(64), index=True, unique=True)
- email = db.Column(db.String(120), index=True, unique=True)
- password_hash = db.Column(db.String(128))
- posts = db.relationship('Post', lazy='dynamic', back_populates='author')
- def __repr__(self):
- return '<User {}>'.format(self.username)
- class Post(db.Model):
- id = db.Column(db.Integer, primary_key=True)
- body = db.Column(db.String(140))
- timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow)
- author_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
- author = db.relationship('User')
- def __repr__(self):
- return '<Post {}>'.format(self.body)
- db.drop_all()
- db.create_all()
- # Tony will be both created and added to the session
- u1 = User(username='tony', email='tony@example.com')
- db.session.add(u1)
- # Jake will be created, but not added
- u2 = User(username='jake', email='jake@example.com')
- # Create a post by Jake
- p1 = Post(body='this is my post!', author=u1)
- # Add Tony's post to the session
- db.session.add(p1)
- # Create a post by tony, since tony does not yet exist as a user, he is created automatically
- p2 = Post(body='this is my post!', author=u2)
- # Add tony's post to the session
- db.session.add(p2)
- # After the session has everything defined, commit it to the database
- db.session.commit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement