使用SQLAlchemy查询防止重复

时间:2014-02-19 10:54:12

标签: python sqlalchemy duplicates python-2.x

我有以下功能:

@periodic_task(run_every=crontab(minute="*/3"))
def PeriodicUpdateStockItems():
    #Query to get data from Sims table
    res = Session.query(
        Sims.purchase_order, func.count(Sims.iccid), Sims.create_stamp, Sims.status, Sims.network
    ).filter(
        Sims.stock_sets_id == None
    ).filter(
        Sims.purchase_order != None
    ).group_by(Sims.purchase_order).first()
    print res
    # print Session.query(StockSets.purchase_order).filter(StockSets.purchase_order == res.purchase_order).first()
    if res:
        if not Session.query(StockSets.id).filter(StockSets.purchase_order == res.purchase_order).first() and not Session.query(StockSets.purchase_order).filter(StockSets.purchase_order == res.purchase_order).first():
            print "GOTHERE"
            # Instantiate StockSets table to insert new data 
            newss = StockSets()
            newss.purchase_order = res.purchase_order
            newss.sim_count = res[1]
            newss.create_stamp = res.create_stamp
            newss.status = res.status
            newss.provider_id = res.network
            # Attempt to prevent Duplicate entry from being inserted
            if not Session.query(StockSets.purchase_order).filter(StockSets.purchase_order == newss.purchase_order).first():
                print "GOTHERE-"
                Session.add(newss)
                transaction.commit()

此函数从一个表中获取数据并将其插入到新表中。我想防止条目重复。

我尝试添加2个查询:

Session.query(
    StockSets.purchase_order
).filter(
    StockSets.purchase_order == newss.purchase_order
).first()

和:

Session.query(
    StockSets.purchase_order
).filter(
    StockSets.purchase_order == res.purchase_order
).first()

但这没有效果,因为当我尝试将它们打印出来时它返回None 如何构建查询以防止重复条目?

2 个答案:

答案 0 :(得分:1)

首先,如果您想确保没有重复的条目,我认为通过创建多列唯一性约束而不是在插入和更新代码中,您可以执行此操作。

答案 1 :(得分:0)

我找到了解决方案:

@periodic_task(run_every=crontab(minute="*/5"))
def PeriodicUpdateStockItems():
    """
    A task to add Stock items to the stockstets table and to update the stock_sets_id in the sims table
    """
    res = Session.query(
        Sims.purchase_order, func.count(Sims.iccid), Sims.create_stamp, Sims.status, Sims.network
    ).filter(
        Sims.stock_sets_id == None
    ).filter(
        Sims.purchase_order != None
    ).group_by(Sims.purchase_order).first()
    log.debug(res)
    stock_sets_id = None
    if res:
        stockset = Session.query(
            StockSets.id
        ).filter(
            StockSets.purchase_order == res.purchase_order
        ).first()
        if not stockset:
            stockset = StockSets()
            stockset.purchase_order = res.purchase_order
            stockset.sim_count = res[1]
            stockset.create_stamp = res.create_stamp
            stockset.status = res.status
            stockset.network = res.network
            Session.add(stockset)
            transaction.savepoint()
            stock_sets_id = stockset.id
            log.debug(stock_sets_id)
            transaction.commit()
        else:
            stock_sets_id = stockset.id
            log.debug(stock_sets_id)
        Session.query(
            Sims
        ).filter(
            Sims.purchase_order == res.purchase_order
        ).update(
            {'stock_sets_id': stock_sets_id}
        )
        transaction.commit()

我没有充分利用我的sqlalchemy查询,第一个查询实际上是我需要检查的重复项:

        res = Session.query(
            Sims.purchase_order, func.count(Sims.iccid), Sims.create_stamp, Sims.status, Sims.network
        ).filter(
            Sims.stock_sets_id == None
        ).filter(
            Sims.purchase_order != None
        ).group_by(Sims.purchase_order).first()

有了这个我阻止复制(在插入​​时),我正在同一个更新表。在问题中,我在另一个函数中进行了更新。此函数的目的是将数据插入一个表(无重复条目),并使用stock_sets_id更新另一个表。

@ACV感谢您对此问题的回复。

我希望这可以在将来帮助某人。