text: import os
  
  from datetime import datetime
  
  from flask import Flask
  from flask_sqlalchemy import SQLAlchemy
  
  
  # 초기화
  app = Flask(__name__)
  
  # 초기화 - SQLAlchemy
  app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' \
      + os.path.join(os.path.abspath(os.path.dirname(__file__)), 'app.db')
  app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
  app.config['SQLALCHEMY_ECHO'] = True
  
  db = SQLAlchemy(app)
  
  
  # 반드시 Post 모델 위에 선언해야 한다.
  post_tags = db.Table('post_tags', 
      db.Column('tag_id', db.Integer, db.ForeignKey('tag.id')),
      db.Column('post_id', db.Integer, db.ForeignKey('post.id'))
  )
  
  
  # 모델 정의
  class Post(db.Model):
      id = db.Column(db.Integer, primary_key=True)
      title = db.Column(db.String(80))
      body = db.Column(db.Text)
      pub_date = db.Column(db.DateTime)
  
      # many-to-many 참조 관계 정의
      # 양방향 모두 lazy='dynamic'이므로 반환 객체에 all() 메소드를 사용해야 실제 쿼리 실행
      tags = db.relationship('Tag', secondary=post_tags, backref=db.backref('posts', lazy='dynamic'), lazy='dynamic')
      
      def __init__(self, title, body, category, pub_date=None):
          self.title = title
          self.body = body
          if pub_date is None:
              pub_date = datetime.utcnow()
          self.pub_date = pub_date
          self.category = category
  
      def __repr__(self):
          return '<Post %r>' % self.title
  
  class Tag(db.Model):
      id = db.Column(db.Integer, primary_key=True)
      name = db.Column(db.String(50))
      
      def __init__(self, name):
          self.name = name
  
      def __repr__(self):
          return '<Tag %r>' % self.name
  
  
  # 뷰 함수와 라우팅
  @app.route('/')
  def index():
      p = Post.query.first()
      print(p.tags.all())
      
      t = Tag.query.first()
      print(t.posts.all())
  
      return '<h1>Many-to-Many</h1>'
  
  # 서버 실행
  if __name__ == '__main__':
      app.host = '127.0.0.1'
      app.port = 5000
      app.debug = True
      
      app.run()
  