-
-
Notifications
You must be signed in to change notification settings - Fork 29
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
Saving an object removes all links to its related objects #51
Comments
I believe you are looking for: If you read the Models page on the wiki, at the top it mentions the scheme used when "inflecting" relation names. Also, for what it's worth, given a class you can find the generated name for that class via Also, take note that Hope this is helpful. |
You can only use "add" on that table relation if it has a "has_many". Code for example "has_one" Value and add is used for "has_many" situations. See the example again.
|
Ok. Got it. Thank you for explaining index mode features. But what happens after calling .save() on code? code=Code.create(code='sample')
code['value']=Value.create(value=10)
print(code['value']['value']) # prints 10
code.save()
print(code['value']) # prints None Where to my |
I experimented with your example a bit. Indeed, the way you are using it doesn't seem to be working, and may very well be considered a bug as that pattern of use isn't very far-fetched, however, if you re-arrange your code slightly, you can achieve what you are after. For example (slightly simplified for this example discussion): from remodel.models import Model
from remodel.helpers import create_tables, create_indexes, drop_tables
class Code(Model):
has_one=('Value',)
class Value(Model):
belongs_to=('Code',)
create_tables(); create_indexes()
code=Code.create(code='sample')
# Rather than setting the Value to the Code, as you were doing via:
# `code['value']=Value.create(value=10)`
# Do the following instead… either:
value = Value.create(value=10, code=code)
# …no save necessary here - create automatically persists the record
print(value['code']) #--> <Code: […id…]>
print(code['value']) #--> <Value: […id…]>
# or you can also do it this way I believe:
value = Value.create(value=10)
value['code'] = code
value.save() The gist here I think is that the association must be made on the "belonging" object, not the "has-ing" object in the relationship. I was genuinely surprised that your initial code didn't work as expected, as that pattern of use seems reasonable to me. Maybe @linkyndy can shed some light or agree that what you tried was legit and that what you found is a "bug". |
Indeed, I've spotted a bug on that part. I wanted to flush related caches, but unfortunately I am also wiping existing descriptors defined on the field handler itself. I'll start working on a fix for this issue. Thanks for reporting it! |
Actually, after more thinking on this, I am inclined to say that the current behaviour is the intended one. Let me explain. In the above example, we are operating on a I admit I've borrowed a few concerns from Django's relationship model, that is, With this in mind, the above example can be made "right": code=Code.create(code='sample')
value=Value.create(value=10) # We need this object to save it after it's being assigned to `code`
code['value']=value # This sets `code_id` on `value`
print(code['value']['value']) # prints 10
value.save() # This ensures `value` is saved with `code_id` set
code.save()
print(code['value']['value']) # prints 10 The difference vs the former example is that previously, This is my view on this specific case, and on remodel's relationship model. Let me know if that makes sense to you as well, and if not, let's discuss potential improvements related to this! |
I trying to repeat a demo tutorial with code:
And this code raises error:
Whats wrong with code?
The text was updated successfully, but these errors were encountered: