Issue: ArrayField and explicit type casts error
Author: joaodlf
State: closed
Created: 2017-10-02 14:41:10

Hi, I'm using the latest version of peewee with Python 3.6, here is my code:

```
from playhouse.postgres_ext import *

from config_constants import SQL_HOST, SQL_USER, SQL_PASSWORD, SQL_DATABASE

database = PostgresqlExtDatabase(SQL_DATABASE,
                                 user=SQL_USER,
                                 password=SQL_PASSWORD,
                                 host=SQL_HOST,
                                 register_hstore=False)


class BaseModel(Model):
    class Meta:
        database = database


class Extras(BaseModel):
    id = PrimaryKeyField()
    utm_campaign = ArrayField(CharField)
    utm_medium = ArrayField(CharField)
    utm_source = ArrayField(CharField)
    utm_term = ArrayField(CharField)

    class Meta:
        db_table = "extras"

where = [Extras.utm_source.contains("GoogleAdwords")]

extras = Extras \
    .select() \
    .where(*where)

for extra in extras:
    print(extra)
```

This is returning me the following:

```
Traceback (most recent call last):
  File "/Users/<snip>/env/lib/python3.6/site-packages/playhouse/postgres_ext.py", line 383, in execute_sql
    cursor.execute(sql, params or ())
psycopg2.ProgrammingError: operator does not exist: text[] @> character varying[]
LINE 1: ...erm" FROM "extras" AS t1 WHERE ("t1"."utm_source" @> ARRAY['...
                                                             ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test.py", line 48, in <module>
    for extra in extras:
  File "/Users/<snip>/env/lib/python3.6/site-packages/peewee.py", line 3240, in __iter__
    return iter(self.execute())
  File "/Users/<snip>/env/lib/python3.6/site-packages/peewee.py", line 3233, in execute
    self._qr = ResultWrapper(model_class, self._execute(), query_meta)
  File "/Users/<snip>/env/lib/python3.6/site-packages/peewee.py", line 2912, in _execute
    return self.database.execute_sql(sql, params, self.require_commit)
  File "/Users/<snip>/env/lib/python3.6/site-packages/playhouse/postgres_ext.py", line 390, in execute_sql
    self.commit()
  File "/Users/<snip>/env/lib/python3.6/site-packages/peewee.py", line 3598, in __exit__
    reraise(new_type, new_type(*exc_args), traceback)
  File "/Users/<snip>/env/lib/python3.6/site-packages/peewee.py", line 135, in reraise
    raise value.with_traceback(tb)
  File "/Users/<snip>/env/lib/python3.6/site-packages/playhouse/postgres_ext.py", line 383, in execute_sql
    cursor.execute(sql, params or ())
peewee.ProgrammingError: operator does not exist: text[] @> character varying[]
LINE 1: ...erm" FROM "extras" AS t1 WHERE ("t1"."utm_source" @> ARRAY['...
                                                             ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.
```

I'm assuming I'm doing something wrong here but can't put my finger on it.

This is an issue when I attempt to iterate through the results, the "extras" variable actually does return an **playhouse.postgres_ext._Array** object.

Querying for fields that are not of ArrayField type works as expected with no errors.

Also, running a query in psql `SELECT * FROM extras WHERE utm_source @> ARRAY['Google Adwords'];` utm_source (like all other array fields in this example) is of type _text[]_.

The fix to this was to update ArrayField(CharField) to ArrayField(TextField), since my fields are of type **text[]**. Doh.