> -----Original Message-----
> From: [email protected] [mailto:[email protected]]
> On Behalf Of jln
> Sent: 15 March 2011 16:37
> To: sqlalchemy
> Subject: [sqlalchemy] In-memory object duplication
>
[SNIP]
> statuses = OneToMany('DocumentStatus', inverse='doc', cascade='all,
> delete-orphan', order_by=['timestamp'])
>
> So, when I create a new DocumentStatus object, Document.statuses
> lists
> two of them, but not actually persisted to the database. In other
> words, leaving my Python shell, and starting the model from scratch,
> there actually is only one child object (corroborated by squizzing
> the
> database directly). Here's my DocumentStatus.create() class method:
>
> @classmethod
> @logged_in
> @log_input
> def create(cls, doc, status, person=None, date=None):
> person=validate_person(person)
> if person:
> status = DocumentStatus(doc=doc, status=status,
> person=person, date=resolve_datetime(date))
> if status:
> doc.statuses.append(status)
> doc.flush()
> out = 'Document status created'
> success = True
> else:
> out = 'Document status not created'
> success = False
> else:
> out = 'Person does not exist'
> success = False
> log_output(out)
> return success
>
> I simply don't know why this is happening or, as I said, how to
> search, intelligently, for an answer.
I don't know Elixir, but I assume that the "inverse='doc'" line in the
relationship sets up an SQLAlchemy backref. If so, then setting
status.doc (presumably done in DocumentStatus.__init__) will
automatically populate doc.statuses at the same time.
So when you do doc.statuses.append(status) a bit later on, you're adding
it to the list a second time.
Hope that helps,
Simon
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en.