django-environ icon indicating copy to clipboard operation
django-environ copied to clipboard

default should be used when DATABASE_URL is empty string and default is set

Open jeverling opened this issue 2 years ago • 1 comments

When DATABASE_URL == "", django-environ will return an empty dict if the default argument has been passed. If the variable doesn't exist, the default argument is correctly used.

E.g. consider this:

DATABASES = {
    "default": env.db_url(
        "DATABASE_URL", default=f"sqlite:///{BASE_DIR / 'db.sqlite3'}"
    )
}

When DATABASE_URL is not set:

{'default': {'NAME': '/app/db.sqlite3',
  'USER': '',
  'PASSWORD': '',
  'HOST': '',
  'PORT': '',
  'ENGINE': 'django.db.backends.sqlite3'}}

When DATABASE_URL is set to empty string: {'default': {}}

I think it would be better if an empty string for DATABASE_URL value would lead to the same behavior as when the variable isn't set. docker-compose for example defaults to empty strings when a variable isn't set.

jeverling avatar Aug 11 '22 22:08 jeverling

@jeverling Although I guess a quick checking on the var value to coerce empty to None would safely do, I'd like to point you to relevant related issues which are similar to yours: #192, #209 (almost identical, but for int), #227, #451.

Your issue it's a very good example of the problem discussed there in more detail. I'd suggest you to add your case there to provide another example, since it's likely to get more attention (and also close this issue as duplicate).

HTH.

pataquets avatar Apr 30 '24 16:04 pataquets