alfonsodg / demo-web2py

Apache License 2.0
0 stars 0 forks source link

GAE reference properties can be integers or strings #16

Closed alfonsodg closed 10 years ago

alfonsodg commented 10 years ago

From robi...@gmail.com on June 19, 2008 14:03:04

obj1 = FirstModel() obj1.put() obj.key() datastore_types.Key.from_path('FirstModel', 2, _app=u'web2py') obj.key().kind() 'FirstModel' obj.key().id() 2 obj.key().name() None obj.key().id_or_name() 2

obj1 = FirstModel(key_name="xyz123") obj1.put() obj.key() datastore_types.Key.from_path('FirstModel', u'xyz123', _app=u'web2py') obj.key().kind() 'FirstModel' obj.key().id() None obj.key().name() 'zyz123' obj.key().id_or_name() 'zyz123'

A key can be encoded to a string by passing the Key object to str() (or calling the object's str() method). A string-encoded key is an opaque value using characters safe for including in URLs. The string-encoded key can be converted back to a Key object by passing it to the Key constructor (the encoded argument).

str(key) 'agZ3ZWIycHlyEAsSCkZpcnN0TW9kZWwYBww'

Key('agZ3ZWIycHlyEAsSCkZpcnN0TW9kZWwYBww') datastore_types.Key.from_path('FirstModel', 7L, _app=u'web2py')

I would say reference properties should be strings determined by str(key), so that keys uniquely identify (Kind,id_or_name,_app)

Should be able to directly assign a record to a reference type, and str(record.key()) is called to set the reference property.

Should also be able to do some type checking by verifying the key.kind() of the assigned record matches the key.kind() of the reference property.

Original issue: http://code.google.com/p/web2py/issues/detail?id=16

alfonsodg commented 10 years ago

From robi...@gmail.com on June 19, 2008 12:06:07

If strings are used for references, then primary id's should be strings record.id = str(record.key()) too.

alfonsodg commented 10 years ago

From robi...@gmail.com on June 19, 2008 12:13:21

To support entity groups new records need to accept a 'parent=' attribute that is some key or existing record. The parent cannot be changed later as it becomes part of the key:

obj = FirstModel()
obj.put()

obj1 = FirstModel(parent=obj)
obj1.put() 

obj1.key() datastore_types.Key.from_path('FirstModel', 15, 'FirstModel', 16, _app=u'web2py')

alfonsodg commented 10 years ago

From massimod...@gmail.com on June 19, 2008 12:46:47

The non-GAE version of the web2py ORM uses int for reference and for primary id. What is wrong with the current implementation of using obj.key().id()?

alfonsodg commented 10 years ago

From robi...@gmail.com on June 19, 2008 13:45:07

In GAE, ReferenceProperty is of type Key.

Sorry, I meant record.reference should be some form of Key, record.id should remain int.

Does the current version check for compatible type (table/kind) when assigning references?

record.id -> record.key().id() (int) (compatible) record.reference -> other.key() (ReferenceProperty) (type checked)

Is there currently a way to access record.key() outside the ORM?

alfonsodg commented 10 years ago

From massimod...@gmail.com on June 19, 2008 19:44:11

the answer is no to both questions

alfonsodg commented 10 years ago

From robi...@gmail.com on June 19, 2008 20:17:13

Reference types should use ReferenceProperty and do type checking. The key() should be exposed outside the ORM. Some parent= field should allow for setting the entity group.

Would these be sensible next steps for gql.py?

alfonsodg commented 10 years ago

From massimod...@gmail.com on June 20, 2008 09:31:22

This would make it not compatible ith non-GAE backend. Anyway, send me a patch and I will look into it. I am not sure I understand exactly what you are asking.

alfonsodg commented 10 years ago

From robi...@gmail.com on June 29, 2008 17:38:32

I got the gql doctests to run, then got the failures down to 28/64 failures/total. (patch attached). The next step is to pass datetimes as parameters or find a serialization format: http://timezones.appspot.com/ The goal should be to get all the doctests passing, then refactor gql.py to remove redundancy and add new features.

References should make use of ReferenceProperty instead of raw ids. The key() should be exposed outside the ORM. A parent= field should allow for setting a new record's entity group.

Every record in GAE is in an entity group. You specify the entity group setting parent=some_record. Records that are in the same entity group can be modified inside if a single transaction, so it is important to support a parent= field.

Attachment: gql_doctest.patch

alfonsodg commented 10 years ago

From robi...@gmail.com on July 02, 2008 10:58:48

This file (gluon/contrib/gae.py):

-switched from GQL to GAE API -got the valid doctests to pass -references use ReferenceProperty -insert(parent=Key) is already passed along to _tableobj

Caveats: -GAE API does not do != or IN (they are syntactic sugar for multiple queries) -dates, times, and datetimes return in UTC, like gql.py

Todo: -use google_db.Model.count() -refactor, remove leftover sql stuff -somehow expose entity.key() in results -set tzinfo or convert returned datetimes from UTC to localtime -add datetime roundtrip doctests, currently they would fail

Robin

Attachment: gae.py

alfonsodg commented 10 years ago

From robi...@gmail.com on July 03, 2008 12:31:01

-got set chaining working -cleaned up sql leftovers -got count working -handle id>0 special case -added more error handling -added many doctests

Todo: datetime to localtime unicode tests

Attachment: gql2_py.patch

alfonsodg commented 10 years ago

From robi...@gmail.com on July 03, 2008 12:38:25

The first patch does not seem to contain diffs, only tree info.

This patch contains the diffs.

Robin

Attachment: gql2_py.patch

alfonsodg commented 10 years ago

From massimod...@gmail.com on July 05, 2008 06:53:45

Patch applied (svn trunk, not yet in launchapd)

alfonsodg commented 10 years ago

From robi...@gmail.com on July 06, 2008 15:45:32

This patch: bzr diff gluon/contrib/gql2.py > gql2_py_dates.patch

references ReferenceProperty ==> IntegerProperty set the 'TZ' for doctests added basic datetime doctests

Attachment: gql2_py_dates.patch

alfonsodg commented 10 years ago

From massimod...@gmail.com on July 10, 2008 08:13:49

All Your patches have been incorporated. Thanks

Status: Fixed