[sqlalchemy] Custom type does not seem to honour is_mutable

2010-08-13 Thread Dan
I have created a custom type in order to store denormalized PKs in a
TEXT field. The idea is that the text is converted back and forth from
a set of integers:

http://paste.pocoo.org/show/249784/

This seems to work OK, however if you make a change to the set it's
not picked up by SQLAlchemy on commit.

For example, given the following model:

class Post(DeclarativeBase):
__tablename__ = posts
id = Column(Integer, primary_key=True)
votes = Column(DenormalizedText)

def __init__(self, *args, **kwargs):
super(Post, self).__init__(*args, **kwargs)
self.votes = self.votes or set()

If I do this:

post = Post()
post.votes.add(3)

session.add(post)
session.commit()

The value '3' is committed to the 'votes' column as expected.

However if I then try to modify:

post.votes.add(5)
session.commit()

The change to the set is not saved to the DB, i.e. it's still 3.

-- 
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.



Re: [sqlalchemy] Custom type does not seem to honour is_mutable

2010-08-13 Thread Michael Bayer

On Aug 13, 2010, at 10:01 AM, Dan wrote:

 I have created a custom type in order to store denormalized PKs in a
 TEXT field. The idea is that the text is converted back and forth from
 a set of integers:
 
 http://paste.pocoo.org/show/249784/

this is unrelated, but the code is incorrect there, should be

def process(value):
if value is not None:
items = [str(item).strip() for item in value]
value = self.separator.join(item for item in items if item)

otherwise, you must implement copy_value() on your type.   Here, the value 
isn't being copied so there's nothing to compare to.

Usually you're supposed to mixin MutableType which will raise notimplemented 
for copy_value().   I guess still more docs are needed since you were misled by 
the is_mutable() method.






 
 This seems to work OK, however if you make a change to the set it's
 not picked up by SQLAlchemy on commit.
 
 For example, given the following model:
 
 class Post(DeclarativeBase):
__tablename__ = posts
id = Column(Integer, primary_key=True)
votes = Column(DenormalizedText)
 
def __init__(self, *args, **kwargs):
super(Post, self).__init__(*args, **kwargs)
self.votes = self.votes or set()
 
 If I do this:
 
 post = Post()
 post.votes.add(3)
 
 session.add(post)
 session.commit()
 
 The value '3' is committed to the 'votes' column as expected.
 
 However if I then try to modify:
 
 post.votes.add(5)
 session.commit()
 
 The change to the set is not saved to the DB, i.e. it's still 3.
 
 -- 
 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.
 

-- 
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.