0

I use flask, an api and SQLAlchemy with SQLite. I begin in python and flask and i have problem with the list.

My application work, now i try a news functions. I need to know if my json informations are in my db. The function find_current_project_team() get information in the API.

def find_current_project_team():
    headers = {"Authorization" : "bearer "+session['token_info']['access_token']}
    user = requests.get("https://my.api.com/users/xxxx/", headers = headers)
    user = user.json()
    ids = [x['id'] for x in user]
    return(ids)

I use ids = [x['id'] for x in user] (is the same that) :

ids = []
for x in user:
    ids.append(x['id'])

To get ids information. Ids information are id in the api, and i need it. I have this result :

[2766233, 2766237, 2766256]

I want to check the values ONE by One in my database. If the values doesn't exist, i want to add it. If one or all values exists, I want to check and return "impossible sorry, the ids already exists".

For that I write a new function:

def test():
    test = find_current_project_team()
    for find_team in test:
        find_team_db = User.query.filter_by(
            login=session['login'], project_session=test
        ).first()

I have absolutely no idea to how check values one by one.

If someone can help me, thanks you :)

Actually I have this error :

sqlalchemy.exc.InterfaceError: (InterfaceError) Error binding parameter 1 - probably unsupported type. 'SELECT user.id AS user_id, user.login AS user_login, user.project_session AS user_project_session \nFROM user \nWHERE user.login = ? AND user.project_session = ?\n LIMIT ? OFFSET ?' ('my_tab_login', [2766233, 2766237, 2766256], 1, 0)

SuperShoot
  • 9,880
  • 2
  • 38
  • 55
fenatu
  • 11
  • 3

2 Answers2

0

It looks to me like you are passing the list directly into the database query:

def test():
    test = find_current_project_team()
    for find_team in test:
        find_team_db = User.query.filter_by(login=session['login'], project_session=test).first()

Instead, you should pass in the ID only:

def test():
    test = find_current_project_team()
    for find_team in test:
        find_team_db = User.query.filter_by(login=session['login'], project_session=find_team).first()

Asides that, I think you can do better with the naming conventions though:

def test():
    project_teams = find_current_project_team()
    for project_team in project_teams:
        project_team_result = User.query.filter_by(login=session['login'], project_session=project_team).first()
HAKS
  • 419
  • 4
  • 9
  • Thanks bro. It some work, now i have the new mistake to add the result in my db (i think it dosent match with my user table) ``` sqlalchemy.exc.IntegrityError: (IntegrityError) UNIQUE constraint failed: user.login 'INSERT INTO user (login, project_session) VALUES (?, ?)' ('salty', '2766233') ``` – fenatu Aug 27 '19 at 09:24
  • Can you add the code snippet for your model to the question? – HAKS Aug 27 '19 at 09:27
0

All works thanks

My code :

    project_teams = find_current_project_team()
    for project_team in project_teams:
        project_team_result = User.query.filter_by(project_session=project_team).first()
        print(project_team_result)
        if project_team_result is not None:
            print("not none")
        else:
            project_team_result = User(login=session['login'], project_session=project_team)
            db.session.add(project_team_result)
            db.session.commit()
fenatu
  • 11
  • 3