Skip to content

Instantly share code, notes, and snippets.

@berlotto
Created April 10, 2019 18:45
Show Gist options
  • Save berlotto/d7ee90697e363cb08f133ff37414b992 to your computer and use it in GitHub Desktop.
Save berlotto/d7ee90697e363cb08f133ff37414b992 to your computer and use it in GitHub Desktop.
Campo ArrayField compatível com SQLite
"""
Esta classe compatibiliza o campo ArrayField do Postgres para rodar na migration do SQLite.
Django 1.9
O correto é você testar em um banco de dados Postgres mesmo, mas para ambientes onde você execute os dois
testes, ajuda.
"""
from django.contrib.postgres.fields import ArrayField
class CustomArrayField(ArrayField):
"""Classe criada somente para compatibilizar o campo ArrayField do Postgres
com o banco de dados SQLite para fins de testes unitários em SQLite.
"""
def db_type(self, connection):
size = self.size or ''
if 'sqlite' in connection.settings_dict['ENGINE']:
ret = '%s[%s]' % (self.base_field.db_type(connection), size)
if ret and ret.endswith('[]'):
ret = ret[:-2]
else:
ret = '%s[%s]' % (self.base_field.db_type(connection), size)
return ret
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment