dynamorm icon indicating copy to clipboard operation
dynamorm copied to clipboard

OneToOne Caching Issue

Open ricky-sb opened this issue 5 years ago • 1 comments

When defining a OneToOne relationship, we cache the result into the other_inst variable here.

However, other_inst remains the same for the entire Model, not the individual object of the Model.

Example:

from dynamorm import DynaModel, OneToOne

from marshmallow.fields import String


class Device(DynaModel):
    class Table:
        name = 'Device'
        hash_key = 'device_id'

    class Schema:
        device_id = String(required=True)
        device_name = String()
        device_location = String()


class User(DynaModel):
    class Table:
        name = 'User'
        hash_key = 'user_id'

    class Schema:
        user_id = String(required=True)
        device_id = String(required=True)

    device = OneToOne(
        Device,
        query=lambda user: dict(device_id=user.device_id),
    )


Device(device_id='Device_1').save()
Device(device_id='Device_2').save()

User(user_id='Alice', device_id='Device_1').save()
User(user_id='Bob', device_id='Device_2').save()

alice = User.get(user_id='Alice')
assert alice.device.device_id == 'Device_1'

bob = User.get(user_id='Bob')
assert bob.device.device_id == 'Device_2'

ricky-sb avatar Nov 16 '19 00:11 ricky-sb

This is another solution

https://gist.github.com/frdteknikelektro/771f191013ad3e34c3521b8bec11e0fb

This code solve the issue, the problem is on python Descriptor itself. Descriptor is attach to Class not Instance.

Or any other solution is in Descriptor class do not use any cache like other_inst

frdteknikelektro avatar Feb 16 '20 11:02 frdteknikelektro