Saving an object removes all links to its related objects
See original GitHub issueI trying to repeat a demo tutorial with code:
from remodel.models import Model
from remodel.helpers import create_tables, create_indexes
class Products(Model):
has_many=('Code',)
has_one=('Value',)
class Code(Model):
has_one=('Value',)
class Value(Model):
belongs_to_many=('Product', 'Code',)
create_tables()
create_indexes()
code=Code.create(code='sample')
code['value'].add(Value.create(value=10))
And this code raises error:
Traceback (most recent call last):
File "models-test.py", line 18, in <module>
code['value'].add(Value.create(value=10))
AttributeError: 'NoneType' object has no attribute 'add'
Whats wrong with code?
Issue Analytics
- State:
- Created 7 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Linked objects and embedded objects
When an object is linked, information can be updated if the source file is modified. Linked data is stored in the source file....
Read more >Object cannot be deleted do to invisible link
Yes, you can easily get that to happen: delete the in-links, delete the object and save the target module, then close the source...
Read more >Related objects reference
Using remove() with a many-to-many relationship, however, will delete the relationships using QuerySet.delete() which means no model save() methods are called; ...
Read more >Control Access to Objects Unit - Trailhead - Salesforce
To access a detailed screen reader version of this unit, click the link below: ... laws require recruitment-related records be saved for a...
Read more >Custom objects
Custom objects allow you to store additional data in a scalable manner and link that data to a contact or account record. You...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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
Code
object. When saving this object, we’re interested in saving fields for this actual object, not also its related objects. This would raise a few concerns, if considered: what happens if the related object is invalid? Should the whole operation fail (so, even theCode
object will not be saved)? What happens if that object is already saved/not saved yet?I admit I’ve borrowed a few concerns from Django’s relationship model, that is,
HasMany
andHasAndBelongsToMany
relations are being updated on assignment, whileHasOne
andBelongsTo
aren’t – not even when the parent object is saved. This is in contrast with other frameworks, such as Ruby on Rails, where a lot more magic is involved. I’ve opted for something closer to Django’s relationship model because it is more straight-forward and the developer controls operations in their entirety (so, no “magic” saves involved).With this in mind, the above example can be made “right”:
The difference vs the former example is that previously,
value
was never saved withcode_id
set, since this field got set aftervalue
was saved (part of its creation). Now, by explicitly saving thevalue
object, we make sure its “ties” to thecode
object are properly persisted.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!