6

For this situation assume there is a table declared with the declarative base called Game, with column names "espn_game_id" and "a_name". There is a session object open under the variable "s". So this works:

s.add(Game(espn_game_id=888, a_name='lol'))
s.commit()

This fails:

n = {"espn_game_id": 888, "a_name": 'lol'}
s.add(Game(n))
s.commit()

This works (assuming there is already an entry with espn_game_id==888):

n = {"a_name": 'lol'}
s.query(Game).filter(Game.espn_game_id==888).update(n)
s.commit()

Why does the second case fail and is there a way to get that type of syntax to work here?

1 Answer 1

15

Try to replace:

s.add(Game(n))

with:

s.add(Game(**n))

Let's assume you have function like this:

def foo(**kwargs):
    print [item for item in kwargs.items()]

It expects keyword not positional arguments so this will work:

foo(**{'foo': 1, 'bar': 2}) 

and this will fail:

foo({'foo': 1, 'bar': 2}) 

For a better explanation you should read *args and **kwargs?

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.