flask-sqlalchemy:如何多次在多对多关系中插入重复记录

时间:2017-06-02 02:28:36

标签: flask sqlalchemy flask-sqlalchemy

我正在使用flask-sqlalchemy并尝试在多对多关系中插入重复记录。但是,只能插入一个项目。例如,请考虑以下代码:

int

现在我想存储调用点db = SQLAlchemy(app) call_global_variable = db.Table('call_global_variable', db.Column('call_id', db.Integer, db.ForeignKey('call.id')), db.Column('global_variable_id', db.Integer, db.ForeignKey('global_variable.id')) ) class Call(db.Model): __tablename__ = 'call' id = db.Column(db.Integer, primary_key = True) callee_id = db.Column(db.Integer, db.ForeignKey('function.id')) callee = db.relationship('Function', foreign_keys = [callee_id]) arg_list = db.relationship('GlobalVariable', secondary = call_global_variable, lazy = 'dynamic') class Function(db.Model): __tablename__ = 'function' id = db.Column(db.Integer, primary_key = True) name = db.Column(db.String(20)) type = db.Column(db.String(20)) def __repr__(self): return self.name class GlobalVariable(db.Model): __tablename__ = 'global_variable' id = db.Column(db.Integer, primary_key = True) name = db.Column(db.String(20)) type = db.Column(db.String(20)) def __repr__(self): return self.name func = Function(name = 'func', type = 'void') gvar0 = GlobalVariable(name = 'a', type = 'int') gvar1 = GlobalVariable(name = 'b', type = 'int') call = Call(callee = func, arg_list = [gvar0, gvar0, gvar0, gvar0, gvar1]) db.session.add_all([func, gvar0, gvar1, call]) db.session.commit() print len(list(call.arg_list)) ,其中func(a, a, a, a, b)是一些函数,funca是全局变量。因此,我希望b包含4个重复的call.arg_listgvar0),因为a出现了4次。但是,a的结果是2.我想原因可能是flask-sqlalchemy删除了重复的len(list(call.arg_list))。所以我的问题是如何声明gvar0Call以及表GlobalVariable的模型,以便我可以向多个人插入多个重复记录(比如call_global_variable)多对多关系gvar0?谢谢!

0 个答案:

没有答案