On Dec 24, 2011, at 3:16 PM, Arturo Sevilla wrote:

> I have an error that occurs after an upgrade to 0.7.4 (from 0.6.8). I use 
> classical mapping but I'm having the following exception when I add it to a 
> session and save() it:
> 
> TypeError: can't pickle function objects 
> 
> If I directly do pickle.dumps(obj) I get the following exception:
> 
> PicklingError: Can't pickle <function remove at 0x4d5ced8>: it's not found as 
> weakref.remove
> 
> I'm getting this for every relationship that does not have uselist=False. For 
> example:
> 
> orm.mapper(User, user, properties={ 'companies': orm.relationship(Company, 
> backref='_admin') })
> 
> For a User and Company classes and a user table with a FK to a company table. 
> This configuration was working with 0.6.8.
> 
> Is it necessary to implement __getstate__() for every class now and remove 
> its InstrumentedList properties?

this is a supported use case and I cannot reproduce your error, so you need to 
supply a full, reproducing test case (also User.companies implies the foreign 
key is on "companies").  See below, use this as a start:

from sqlalchemy import *
from sqlalchemy.orm import *


class User(object):
    def __init__(self, companies):
        self.companies = companies

class Company(object):
    pass

metadata = MetaData()

user= Table('user', metadata, 
    Column('id', Integer, primary_key=True)
)
company= Table('company', metadata, 
    Column('id', Integer, primary_key=True),
    Column('user_id', Integer, ForeignKey('user.id'))
)
mapper(User, user, properties={
    'companies': relationship(Company, backref='_admin') 
})
mapper(Company, company)

e = create_engine("sqlite://", echo=True)
metadata.create_all(e)

s = Session(e)

s.add_all([
    User(companies=[Company(), Company(), Company()])
])
s.commit()

u1 = s.query(User).options(joinedload('companies')).all()

import cPickle
assert cPickle.loads(cPickle.dumps(u1))[0].id == 1


output:

classics-MacBook-Pro:sqlalchemy classic$ python test.py
2011-12-24 18:00:02,263 INFO sqlalchemy.engine.base.Engine PRAGMA 
table_info("user")
2011-12-24 18:00:02,263 INFO sqlalchemy.engine.base.Engine ()
2011-12-24 18:00:02,263 INFO sqlalchemy.engine.base.Engine PRAGMA 
table_info("company")
2011-12-24 18:00:02,264 INFO sqlalchemy.engine.base.Engine ()
2011-12-24 18:00:02,264 INFO sqlalchemy.engine.base.Engine 
CREATE TABLE user (
        id INTEGER NOT NULL, 
        PRIMARY KEY (id)
)


2011-12-24 18:00:02,264 INFO sqlalchemy.engine.base.Engine ()
2011-12-24 18:00:02,264 INFO sqlalchemy.engine.base.Engine COMMIT
2011-12-24 18:00:02,264 INFO sqlalchemy.engine.base.Engine 
CREATE TABLE company (
        id INTEGER NOT NULL, 
        user_id INTEGER, 
        PRIMARY KEY (id), 
        FOREIGN KEY(user_id) REFERENCES user (id)
)


2011-12-24 18:00:02,265 INFO sqlalchemy.engine.base.Engine ()
2011-12-24 18:00:02,265 INFO sqlalchemy.engine.base.Engine COMMIT
2011-12-24 18:00:02,269 INFO sqlalchemy.engine.base.Engine BEGIN (implicit)
2011-12-24 18:00:02,269 INFO sqlalchemy.engine.base.Engine INSERT INTO user 
DEFAULT VALUES
2011-12-24 18:00:02,269 INFO sqlalchemy.engine.base.Engine ()
2011-12-24 18:00:02,270 INFO sqlalchemy.engine.base.Engine INSERT INTO company 
(user_id) VALUES (?)
2011-12-24 18:00:02,270 INFO sqlalchemy.engine.base.Engine (1,)
2011-12-24 18:00:02,270 INFO sqlalchemy.engine.base.Engine INSERT INTO company 
(user_id) VALUES (?)
2011-12-24 18:00:02,271 INFO sqlalchemy.engine.base.Engine (1,)
2011-12-24 18:00:02,271 INFO sqlalchemy.engine.base.Engine INSERT INTO company 
(user_id) VALUES (?)
2011-12-24 18:00:02,271 INFO sqlalchemy.engine.base.Engine (1,)
2011-12-24 18:00:02,271 INFO sqlalchemy.engine.base.Engine COMMIT
2011-12-24 18:00:02,272 INFO sqlalchemy.engine.base.Engine BEGIN (implicit)
2011-12-24 18:00:02,273 INFO sqlalchemy.engine.base.Engine SELECT user.id AS 
user_id, company_1.id AS company_1_id, company_1.user_id AS company_1_user_id 
FROM user LEFT OUTER JOIN company AS company_1 ON user.id = company_1.user_id
2011-12-24 18:00:02,273 INFO sqlalchemy.engine.base.Engine ()
classics-MacBook-Pro:sqlalchemy classic$ 





> 
> Thanks
> 
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To view this discussion on the web visit 
> https://groups.google.com/d/msg/sqlalchemy/-/OsA7xgalXYIJ.
> 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.

Reply via email to