Flask-Sqlalchemy create_all表已被跳过,因为其定义被并发DDL语句修改

时间:2019-06-20 10:43:09

标签: python flask sqlalchemy flask-sqlalchemy

我正在尝试使用flask-sqlalchemy建立多对多关系。但我不断收到错误

  

sqlalchemy.exc.InternalError:(pymysql.err.InternalError)(1684,“表'db'。'user'已被并发DDL语句修改,因此被跳过”)
  [SQL:DESCRIBE user]

我尝试使用db.Table创建表,但是没有运气。我完全遵循flask sqlalchemy website

from flask import Flask
from flask_sqlalchemy import SQLAlchemy

db = SQLAlchemy()


def create_app():
    app = Flask(__name__, instance_relative_config=True)
    app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://user:password@localhost/db?charset=utf8mb4'
    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

    user_tracer = db.Table('user_tracer',
                           db.Column('user_id', db.Integer, db.ForeignKey('user.user_id'), primary_key=True),
                           db.Column('tracer_id', db.Integer, db.ForeignKey('tracer.tracer_id'), primary_key=True)
                           )

    class Tracer(db.Model):
        __tablename__ = 'tracer'
        tracer_id = db.Column(db.Integer(), primary_key=True, nullable=False, unique=True)

    class User(db.Model):
        __tablename__ = 'user'
        user_id = db.Column(db.Integer(), primary_key=True, nullable=False, unique=True)
        tracers = db.relationship('Tracer', secondary=user_tracer, lazy='subquery',
                                  backref=db.backref('users', lazy=True))

    with app.app_context():
        db.init_app(app=app)
        db.create_all()
    return app

0 个答案:

没有答案