2

I'm since las update of GAE Launcher, it creates ID on datastore too big. Like 5330010158992982016, thats a problem to me, because on Javascript these numbers are rounded.

For example, on JS

> a = 533001015899298254645
> 533001015899298270000

an reading a JSON like [{"pk": 5330010158992982016, "model": " .... }],

$.getJSON(' ...

  $.each(data, function(i,item){ ...

    item['pk'] = 533001015899298270000 instead of 533001015899298254645
  }
}

I'm not sure if I'll have the same problem on GAE servers. Any idea to limit ID size?

I'm using Django, but I'm having the same problem with Django and Google Models.

Update:

I found a solution that doesn't force you to change all javascript code of the project. In my case a lot. Like everybody says the best thing is to use de PK (or ID) as a string. But I as using django serializer and in my version and with JSON, the PK is set as a number. The easy solution is change this on the serializer class ( or create a new serializer wich extends original and change this ):

def end_object(self, obj):
       self.objects.append({
            "model"  : smart_unicode(obj._meta),
            "pk"     : smart_unicode(obj._get_pk_val(), strings_only=**False**),
            "fields" : self._current
        })
        self._current = None

Put strings_only to False. It makes the pk on the JSON goes with quotes. All the javascript code works without changes.

The question is... is there any other way to force django serializer to put it as String?

Jgonzabal
  • 53
  • 4
  • I'm also think that you need to use quotes. You need to handle such big IDs because you MAY have them in future. I see this big numbers on my local data too. It's ok. In production they will start from low numbers. – Dmytro Sadovnychyi Apr 02 '13 at 13:49
  • Yeah, I can try it, the problem is that the JSON is created by the django.core.serializers. Probably I Can add the ID. Thanks – Jgonzabal Apr 03 '13 at 12:38

2 Answers2

2

There is no way to read/store this number accurately in JavaScript, since in JavaScript numbers are actually double precision floats and the maximum is 900,719,925,4740,992.

You could

  • return the ids as string instead or
  • start the dev_appserver.py with an argument: --auto_id_policy=sequential
Community
  • 1
  • 1
Lipis
  • 21,388
  • 20
  • 94
  • 121
  • Thanks, probably the best thing for the future is to use Strings adding ID to the serializer. Anyway the --auto_id_policy is great. Thanks – Jgonzabal Apr 03 '13 at 10:41
1

If for some strange reason Lipi's answer didn't cover you you can try another approach which will be to cast all values to strings. So you would have

[{"pk": "5330010158992982016", "model": " .... "}],

I see you are using an Ajax call which probably means you won't need the following part but For you Django variables instead of {{my_id}} you can make it a string like '{{my_id}}' if you are creating your JavaScript variables on render time.

The AppEngine team is aware of the issue and it will be resolved before the roll the update in production.

topless
  • 8,069
  • 11
  • 57
  • 86