Figured it out. I had two self-referential foreign keys on the table, both pointing (obviously) to the primary key, but for some reason I was specifying foreign_keys to the relations, I just removed these arguments to the relations and this has resolved itself.
On Aug 19, 12:28 pm, Oliver Beattie <[email protected]> wrote: > Still not quite sure how this was going wrong exactly (I was setting > the attribute on the new object but it wasn't being passed along to > the values dictionary), but I've sorted it nonetheless. > > Thanks for trying to help with my (extremely vague) request anyway. > > On Aug 19, 12:07 am, Michael Bayer <[email protected]> wrote: > > > > > On Aug 18, 2010, at 12:29 PM, Oliver Beattie wrote: > > > > On Aug 18, 3:39 pm, Michael Bayer <[email protected]> wrote: > > >> Sent from my iPhone > > > >> On Aug 18, 2010, at 8:27 AM, Oliver Beattie <[email protected]> wrote: > > > >>> I'm not entirely sure why this is happening… it seems to work for me > > >>> in nearly all other circumstances so I'm a bit stumped. Basically, I > > >>> have a declarative table which has a character field as its primary > > >>> key (it's not an ID which can be returned by the server), yet > > >>> SQLAlchemy is issuing an INSERT…RETURNING statement for it. > > > >>> The scenario that seems to make it happening is like this. I'm trying > > >>> to create a copy of an existing object, with a new ID. All the > > >>> attributes on the new object should be pulled from the old object > > >>> (which I'm doing through use of the iterate_properties iterator along > > >>> with setattr, getarre calls), apart from this primary key field (name) > > >>> which is a character field and will be something different. A relation > > >>> on the "old" object will be updated to point at the created object. > > >>> When commit() happens, I get an IntegrityError because it's thinking > > >>> the db will return it the primary key (even though I have explicitly > > >>> set it on the non-persistant instance). > > > >>> Is this a know bug or something I'm doing wrong? > > > >> If the primary key is a character field, you have to ensure the > > >> attribute is populated before the insert occurs. Otherwise sqlalchemy > > >> assumes the value is created by a generator of some kind such as a > > >> column default and issues RETURNING, which is perfectly valid for any > > >> type of column, to get the value back. > > > > Thanks for your reply, Michael — the value is being populated (as in, > > > I am setting the value on the instance before I do session.add()) — is > > > this what you mean? > > > yup. if the correct attribute is populated it will be present in the > > VALUES clause of the INSERT. check your SQL logs to ensure this is the > > case. > > > >>> -- > > >>> 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 > > >>> athttp://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 [email protected]. > > > To unsubscribe from this group, send email to > > > [email protected]. > > > For more options, visit this group > > > athttp://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 [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.
