Issue: peewee.OperationalError: row value misused
Author: kalimalrazif
State: closed
Created: 2017-10-22 01:18:53

Hi, I'm getting this messages when I try to get_or_create a record on db. This is the model on sqlite

```python
class BaseModel(Model):
    class Meta:
        database = db

class Compania(BaseModel):
    name = TextField(null=False)
    url = TextField()
    country = TextField()
    state = TextField()
    city = TextField()
    procesado = BooleanField(default=False)

class Visitados(BaseModel):
    url = TextField()

class Facebook(BaseModel):
    compania = ForeignKeyField(Compania,related_name='facebooks')
    link = ForeignKeyField(Visitados, related_name='enlaces_a_facebooks')
    url = TextField()
```

From previous querys I have link and compania, and pass the objects, when I try to issue this command:
`resp = Facebook.get_or_create(url=url_a_chequear, link=de_donde_viene, compania=que_cia)`

got:
```python
Traceback (most recent call last):
  File "/usr/lib/python3.6/site-packages/peewee.py", line 3830, in execute_sql
    cursor.execute(sql, params or ())
sqlite3.OperationalError: row value misused

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/nomar/Documents/Trabajos/Connexa/hubspot/005.py", line 254, in <module>
    main()
  File "/home/nomar/Documents/Trabajos/Connexa/hubspot/005.py", line 237, in main
    if procesa_url(url_pagina, link, compania) is not None:
  File "/home/nomar/Documents/Trabajos/Connexa/hubspot/005.py", line 158, in procesa_url
    resp = Facebook.get_or_create(url=url_a_chequear, link=de_donde_viene, compania=que_cia)
  File "/usr/lib/python3.6/site-packages/peewee.py", line 5001, in get_or_create
    return query.get(), False
  File "/usr/lib/python3.6/site-packages/peewee.py", line 3220, in get
    return next(clone.execute())
  File "/usr/lib/python3.6/site-packages/peewee.py", line 3274, in execute
    self._qr = ResultWrapper(model_class, self._execute(), query_meta)
  File "/usr/lib/python3.6/site-packages/peewee.py", line 2939, in _execute
    return self.database.execute_sql(sql, params, self.require_commit)
  File "/usr/lib/python3.6/site-packages/peewee.py", line 3837, in execute_sql
    self.commit()
  File "/usr/lib/python3.6/site-packages/peewee.py", line 3656, in __exit__
    reraise(new_type, new_type(*exc_args), traceback)
  File "/usr/lib/python3.6/site-packages/peewee.py", line 135, in reraise
    raise value.with_traceback(tb)
  File "/usr/lib/python3.6/site-packages/peewee.py", line 3830, in execute_sql
    cursor.execute(sql, params or ())
peewee.OperationalError: row value misused
```

Looking into the code notice this is the raw query:
`<class 'modelo.Facebook'> SELECT "t1"."id", "t1"."compania_id", "t1"."link_id", "t1"."url" FROM "facebook" AS t1 WHERE ((("t1"."url" = ?) AND ("t1"."link_id" = (?, ?))) AND ("t1"."compania_id" = ?)) ['https://www.facebook.com/XXXXXXXX', 1, 0, 1]`

Why `("t1"."link_id" = (?, ?))` hast two values?!?!?!

I used peewee to create tables.

Any advice?

Thanks in advance
Nomar



My guess is that the value of `Link` is from a previous call to `get_or_create`, which returns a 2-tuple of `<model instance>, <bool created?>`.

Can you verify whether the value of `Link` is a tuple?

Was that precisely!!! Thanks!!!

If you forget that `get_or_create` returns a tuple and you try to save another model using the result as a field value, you can also get an error like the following:  

`TypeError: int() argument must be a string, a bytes-like object or a number, not 'tuple'`

(Adding this comment as Google-bait so the next person who gets this error finds the answer sooner.)