Closed okraits closed 4 years ago
I did further investigations. A migration like
from django.db import migrations
import django_ipam.base.fields
class Migration(migrations.Migration):
dependencies = [
('hal', '0005_add_setup_target_date'),
]
operations = [
migrations.AddField(
model_name='setup',
name='lan_net',
field=django_ipam.base.fields.NetworkField(help_text='Subnet in CIDR notation, eg: "10.0.0.0/24" for IPv4 and "fdb6:21b:a477::9f7/64" for IPv6', max_length=43),
),
]
leads to the following SQL statements (from python manage.py sqlmigrate hal 0006
):
BEGIN;
--
-- Add field lan_net to setup
--
ALTER TABLE "hal_setup" ADD COLUMN "lan_net" cidr DEFAULT 'None' NOT NULL;
ALTER TABLE "hal_setup" ALTER COLUMN "lan_net" DROP DEFAULT;
COMMIT;
I think that PostgreSQL doesn't like cidr DEFAULT 'None'
here.
If I add default='192.168.1.0/24'
to the definition of the NetworkField in the migration then the migration is executed without errors.
That DEFAULT 'None'
is added implicitly and not in the field definition in Python.
Interesting is also that the default value is added first and then removed again with another ALTER TABLE statement.
I think the empty representation is being passed as "None"
(string) instead of None
(which should be converted to NULL). The column should accept NULL values, but it won't accept empty strings nor "None" strings.
May be this line causing it, but I'm just speculating: https://github.com/openwisp/django-ipam/blob/master/django_ipam/base/fields.py#L56-L57
I think the empty representation is being passed as
"None"
(string) instead ofNone
(which should be converted to NULL). The column should accept NULL values, but it won't accept empty strings nor "None" strings.May be this line causing it, but I'm just speculating: https://github.com/openwisp/django-ipam/blob/master/django_ipam/base/fields.py#L56-L57
Good catch! This also removes the DEFAULT
part in the ALTER TABLE
statement completely.
I already created a PR.
Thank you very much! :+1:
I'm getting errors when running a migration for a NetworkField on PostgreSQL:
I tried setting
blank=True, null=True
and tried settingdefault="192.168.10/24""
but that didn't help.I would be grateful for any hints.