Re: [sqlalchemy] In-memory object duplication
Hi, Simon You nailed it! Thanks a million for the guidance and suggestion - it worked like a bomb. I simply removed the offending append statement and my double entries have disappeared, end the entry still persists to the database. (It seems so simple and obvious with the benefit of insight and hindsight, doesn't it?) Once again, thanks. Greetings On 17 March 2011 17:13, King Simon-NFHD78 simon.k...@motorolasolutions.comwrote: -Original Message- From: sqlalchemy@googlegroups.com [mailto:sqlalchemy@googlegroups.com] On Behalf Of Jacques Naude Sent: 17 March 2011 12:32 To: sqlalchemy@googlegroups.com Subject: Re: [sqlalchemy] In-memory object duplication Hi, Simon Thanks for the quick response. Elixir doesn't use __init__ - there's something automatic going on there. My create(), in essence, does the job of __init__, which means you might still be hitting the nail on the head. I haven't had the time to test it out yet, but I will. (Why, though, would the double entry not be persisted to the database too?) The entry only appears once in the database because SQAlchemy works hard to ensure that a single object instance corresponds to a single row in the database. It doesn't really make sense (in the standard one-to-many model) for a particular child to appear more than once in a parent-child relationship. By default, SA uses a list as the collection implementation for relationships, and doesn't care if you add the same instance more than once. If it bothers you, you could use a set instead: http://www.sqlalchemy.org/docs/orm/collections.html#customizing-collecti on-access Simon -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en. -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
Re: [sqlalchemy] In-memory object duplication
Hi, Simon Thanks for the quick response. Elixir doesn't use __init__ - there's something automatic going on there. My create(), in essence, does the job of __init__, which means you might still be hitting the nail on the head. I haven't had the time to test it out yet, but I will. (Why, though, would the double entry not be persisted to the database too?) Thanks you very much for your response and proposed solution. Will keep you posted. (I was rather hoping for more people to offer guidance in this matter, but there you have it.) Regards Jacques On 15 March 2011 18:51, King Simon-NFHD78 simon.k...@motorolasolutions.comwrote: -Original Message- From: sqlalchemy@googlegroups.com [mailto:sqlalchemy@googlegroups.com] 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 sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en. -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
RE: [sqlalchemy] In-memory object duplication
-Original Message- From: sqlalchemy@googlegroups.com [mailto:sqlalchemy@googlegroups.com] On Behalf Of Jacques Naude Sent: 17 March 2011 12:32 To: sqlalchemy@googlegroups.com Subject: Re: [sqlalchemy] In-memory object duplication Hi, Simon Thanks for the quick response. Elixir doesn't use __init__ - there's something automatic going on there. My create(), in essence, does the job of __init__, which means you might still be hitting the nail on the head. I haven't had the time to test it out yet, but I will. (Why, though, would the double entry not be persisted to the database too?) The entry only appears once in the database because SQAlchemy works hard to ensure that a single object instance corresponds to a single row in the database. It doesn't really make sense (in the standard one-to-many model) for a particular child to appear more than once in a parent-child relationship. By default, SA uses a list as the collection implementation for relationships, and doesn't care if you add the same instance more than once. If it bothers you, you could use a set instead: http://www.sqlalchemy.org/docs/orm/collections.html#customizing-collecti on-access Simon -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
RE: [sqlalchemy] In-memory object duplication
-Original Message- From: sqlalchemy@googlegroups.com [mailto:sqlalchemy@googlegroups.com] 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 sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.