Skip to content

fix: lazy import ArrayField to avoid requiring psycopg2#129

Open
zkksdk wants to merge 1 commit into
Brobin:mainfrom
zkksdk:fix/arrayfield-import-1776443481
Open

fix: lazy import ArrayField to avoid requiring psycopg2#129
zkksdk wants to merge 1 commit into
Brobin:mainfrom
zkksdk:fix/arrayfield-import-1776443481

Conversation

@zkksdk
Copy link
Copy Markdown

@zkksdk zkksdk commented Apr 17, 2026

Fix for Issue #100

Problem

django_seed/guessers.py unconditionally imports ArrayField from django.contrib.postgres.fields, which requires the psycopg2 package even when the project is not using PostgreSQL as its database. This causes ModuleNotFoundError: No module named 'psycopg2' when running seed.py on projects using SQLite or MySQL.

Root Cause

from django.contrib.postgres.fields import ArrayField  # requires psycopg2

Fix

Use try/except ImportError (same pattern already used for JSONField in this file), and guard the isinstance check for when ArrayField is unavailable:

try:
    from django.contrib.postgres.fields import ArrayField
except ImportError:
    ArrayField = None

And in the isinstance check:

if ArrayField and isinstance(field, ArrayField):

Testing

Run seed.py on a Django project that does NOT have psycopg2 installed and does NOT use PostgreSQL. Previously this failed with ModuleNotFoundError. Now it works.

Fixes #100

ArrayField was unconditionally imported from django.contrib.postgres.fields,
which requires psycopg2 even when not using PostgreSQL. This causes
ModuleNotFoundError when using django-seed with non-PostgreSQL databases.

Use try/except ImportError pattern (same as JSONField in this file),
and guard the isinstance check for when ArrayField is unavailable.

Fixes Brobin#100
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

psycopg2 is required for ArrayField even if project isn't using postgres

1 participant