1

according to the below question

SqlAlchemy Case with multiple conditions

how can I use below and_ without get the error of too many values to unpack (expected 2)

  type_case = (
    [
    (Jobs.interview_type == 'adsfas', 0),
    (Jobs.interview_type == 'asdfay', 1),
    (and_(Jobs.interview_type == 'PHONE_SCREEN',
         Jobs.interview_type == 'INCLINED',
         Jobs.name == 'Python',
         Jobs.age >= 18, 2)),
    (and_(Jobs.interview_type == 'adgasd',
         JobOther.interview_type == 'asgasd',
         Jobs.name == 'PHP',
         Jobs.age >= 18, 3))
   ],
   else_=4
  )

  query = db.session.query(
   type_case.label('whatever_type'),
    ).join(
        Jobs,
        Jobs.id == JobOther.id
    )
Elsa
  • 1
  • 1
  • 8
  • 27
  • 4
    Looks like you have a simple typo. You're passing 2 etc. to `and_`, not as the second item in the tuple. Put another way right now you have `and_` surrounded with redundant brackets. Just move 2 and 3 out to form an actual tuple. – Ilja Everilä May 12 '21 at 05:29
  • 3
    ... to elaborate on @IljaEverilä's comment: instead of `Jobs.age >= 18, 2)),` you should have `Jobs.age >= 18), 2),` and the same for another clause. – van May 12 '21 at 06:11
  • @van You are right, thanks so much – Elsa May 13 '21 at 14:27
  • I had the exact same problem! https://stackoverflow.com/questions/69197249/sqlalchemy-case-statement-valueerror-too-many-values-to-unpack-expected-2/69197370#69197370 Had to put the second-last parenthesis before the result value. – gene b. Sep 15 '21 at 17:17

0 Answers0