1

I have a lot of duplicates in the database. To only deliver one (instead of duplicates), I'm trying to use set, but it is returning the duplicates too.

all_orders = Order.query.filter_by(account_id=account.id).all()
orders = set(all_orders)
{377, 348, 353, 377, 354, 377, 356, 377, 357, 377, 359, 378, 358, 378, 361, 378, 357, 378, 364, 378, 363, 378, 378, 362, 364, 379, 355, 379, 366, 379, 367, 379, 368, 379, 358, 379, 369, 379, 358, 379, 369, 379, 337, 370, 379, 338, 370, 379, 339, 343, 371, 379, 347, 371, 379, 372, 379, 372, 379, 373, 379, 373, 379, 374, 379, 374, 379, 375, 379, 375, 379, 376, 379, 376, 379, 376, 379, 376, 379, 376, 379, 377, 379, 377, 379, 377, 379, 377, 379, 377, 380, 377, 381, 377, 377, 377, 377}

EDIT

I am receiving webhook data, and the webhook fired multiple times. I was able to set the sale_id to unique, preventing duplicate data from occurring any further.

3
  • 1
    You have to implement __hash__ and __eq__. Commented Jan 4, 2018 at 19:51
  • What does all_orders look like? Commented Jan 4, 2018 at 19:52
  • 1
    I don't know much about flask, but I imagine the Orders' equality is not based on their id, so different instances are considered different even if they have the same id. Can you do something like Order.query.filter_by(account_id=account.id).unique('id').all() ? Commented Jan 4, 2018 at 19:53

1 Answer 1

7

Use the .distinct() method in your sql alchemy query to get a set-like list.

 ...
 timestamp = calendar.timegm(created.utctimetuple()) # Used to convert time for Intercom   
 orders = Order.query.filter_by(account_id=account.id).distinct()
 print(orders)
 ...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.