O Problema
Em um dos projetos Django que trabalho, precisei utilizar um campo do tipo JSONField.
Aqui está um exemplo de uso:
from django.contrib.postgres.fields import JSONField
from django.db import models
class MyModel(models.Model):
my_field = JSONField(blank=True, null=True)
[...]
O problema foi que isto trouxe erros ao rodar o pytest com a a flag --no-migrations.
Exemplo:
$ pytest --no-migrations
[...]
def _execute(self, sql, params, *ignored_wrapper_args):
self.db.validate_no_broken_transaction()
with self.db.wrap_database_errors:
if params is None:
> return self.cursor.execute(sql)
E psycopg2.errors.UndefinedObject: type "hstore" does not exist
E LINE 1: ...NULL, "product_id" integer NOT NULL, "attributes" hstore NOT...
E
A Solução
Pesquisando sobre o erro na internet, vi que esse era um problema do Postgres. Temos que instalar, manualmente, a extensão hstore.
O passo a passo é esse:
$ sudo -iu postgrespsqlCREATE EXTENSION hstore;\q$ psql -d template1 -c 'create extension hstore;'
Prontinho. Podemos utilizar o --no-migrations 😉