question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Saving an object removes all links to its related objects

See original GitHub issue

I 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:open
  • Created 7 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
linkyndycommented, Jul 31, 2016

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!

0reactions
linkyndycommented, Jun 8, 2019

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 the Code 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 and HasAndBelongsToMany relations are being updated on assignment, while HasOne and BelongsTo 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”:

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, value was never saved with code_id set, since this field got set after value was saved (part of its creation). Now, by explicitly saving the value object, we make sure its “ties” to the code 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!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found