def fix_sequences(self, schema=None, tables=None): for name, cls in sorted(db.Model._decl_class_registry.iteritems(), key=itemgetter(0)): table = getattr(cls, '__table__', None) if table is None: continue elif schema is not None and table.schema != schema: continue elif tables is not None and cls.__tablename__ not in tables: continue # Check if we have a single autoincrementing primary key candidates = [ col for col in table.c if col.autoincrement and col.primary_key ] if len(candidates) != 1 or not isinstance(candidates[0].type, db.Integer): continue serial_col = candidates[0] sequence_name = '{}.{}_{}_seq'.format(table.schema, cls.__tablename__, serial_col.name) query = select( [func.setval(sequence_name, func.max(serial_col) + 1)], table) db.session.execute(query) db.session.commit()
def fix_sequences(): for name, cls in sorted(db.Model._decl_class_registry.iteritems(), key=itemgetter(0)): table = getattr(cls, '__table__', None) if table is None: continue # Check if we have a single autoincrementing primary key candidates = [col for col in table.c if col.autoincrement and col.primary_key] if len(candidates) != 1 or not isinstance(candidates[0].type, db.Integer): continue serial_col = candidates[0] sequence_name = '{}.{}_{}_seq'.format(table.schema, cls.__tablename__, serial_col.name) query = select([func.setval(sequence_name, func.max(serial_col) + 1)], table) db.session.execute(query) db.session.commit()