Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DictionaryField + south schemamigration results in wrong default field value. #57

Closed
lockwooddev opened this issue Aug 6, 2014 · 8 comments
Labels

Comments

@lockwooddev
Copy link

I'm using the latest 1.3.0 release and noticed something odd using the DictionaryField and South (0.8.4) schemamigrations.

I have the following field:

stats = hstore.DictionaryField(
    null=True, schema=[{
        'name': 'total', 'class': 'IntegerField', 'kwargs': {'default': 0}}])

Now when I add a schemamigration for this new field, I end up with a wrong default value on the migration:

def forwards(self, orm):
    # Adding field 'User.stats'
    db.add_column(
        u'users_user', 'stats', self.gf(u'django_hstore.fields.DictionaryField')(default={}, null=True), keep_default=False)

Running this migration results in:

DEBUG 2014-08-06 12:51:22,180 django.db.backends (0.000) ALTER TABLE "users_user" ADD COLUMN "stats" hstore NULL DEFAULT ;; args=[]
FATAL ERROR - The following SQL query failed: ALTER TABLE "users_user" ADD COLUMN "stats" hstore NULL DEFAULT ;
The error was: syntax error at or near ";"
LINE 1: ... TABLE "users_user" ADD COLUMN "stats" hstore NULL DEFAULT ;

The default value on the migration should be a None instead of an empty Dict {}.

To fix this I have to change the default value to None in the forwards migration and in the frozen models.

def forwards(self, orm):
    # Adding field 'User.stats'
    db.add_column(
        u'users_user', 'stats', self.gf(u'django_hstore.fields.DictionaryField')(default=None, null=True), keep_default=False)

Do you know what might be causing this?

@nemesifier
Copy link
Member

for this reason:
https://github.com/djangonauts/django-hstore/blob/master/django_hstore/fields.py#L104

I seem to remember that it was necessary for the schema mode to work but now I don't remember clearly.

I'll investigate asap

@nemesifier
Copy link
Member

removing the forced default does not cause issues.

Setting None as a default value in schema mode does not work and support it would complicate things a bit to no advantage.

Could you verify again with the latest changes?

@lockwooddev
Copy link
Author

Removing the forced default does not affect the schemamigration. Maybe something can be done with south to overcome this issue with it's introspection. I will try later this week to come up with something.

@lockwooddev
Copy link
Author

As a side note: your commit acd1a9b breaks functionality for me.

models.py:352: in serialize
>           'total': self.total,
../env/src/django-hstore/django_hstore/virtual.py:47: in __get__
>       return getattr(instance, self.hstore_field_name).get(self.name, self.default)
E       AttributeError: 'NoneType' object has no attribute 'get'
test_stats.py:486: in test_conditions_true
>       dummy.total = 1000
../env/src/django-hstore/django_hstore/virtual.py:54: in __set__
>       hstore_dictionary[self.name] = value
E       TypeError: 'NoneType' object does not support item assignment

The default of the DictionaryField is turned into a NoneType object and the VirtualFields in the schema cannot be accessed anymore.

@nemesifier
Copy link
Member

that's why the dictionary field should not have default=None, i wrote it in the previous comment

@nemesifier
Copy link
Member

@nemesifier
Copy link
Member

it should be fixed now in the dev version

@nemesifier
Copy link
Member

let us know if you encounter other issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants