Hey,

I had trouble deepcopying model instances that use attribute mapped
collection classes. I'm aware that copying an instance implies a
certain use case with a better solution - I accidentally hit this
error when I had an instance as an attribute on an object I was
deepcopy()ing. The following example fails with "TypeError: attrgetter
expected 1 arguments, got 0".

Is there a way to remedy the situation a bit so the deepcopy doesn't
fail so spectacularly?

Thanks,

N

from sqlalchemy import create_engine, Column, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import scoped_session, sessionmaker, relationship
from sqlalchemy.orm.collections import attribute_mapped_collection
from sqlalchemy.types import Integer, String

import copy

engine = create_engine('sqlite:///:memory:', echo=True)
Base = declarative_base(bind=engine)
Session = scoped_session(sessionmaker(bind=engine))

class Note(Base):
    __tablename__ = 'notes'
    keyword = Column(String, primary_key=True)
    item_name = Column(String, ForeignKey('items.name'))

class Item(Base):
    __tablename__ = 'items'
    name = Column(String, primary_key=True)

    notes = relationship(Note,
collection_class=attribute_mapped_collection('keyword'))

Base.metadata.create_all()

item = Item(name='Foo')

note = Note(keyword='Bar')
item.notes.set(note)

Session.commit()

assert item.notes['Bar'] is note

copy.deepcopy(note) # Passes
copy.deepcopy(item) # Fails

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalch...@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.

Reply via email to