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

OneToOne Caching Issue #87

Open
ricky-sb opened this issue Nov 16, 2019 · 1 comment
Open

OneToOne Caching Issue #87

ricky-sb opened this issue Nov 16, 2019 · 1 comment

Comments

@ricky-sb
Copy link

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'
@frdteknikelektro
Copy link

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

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

No branches or pull requests

2 participants