TypeError: argument of type 'NoneType' is not iterable

837 views
Skip to first unread message

Jakob D.

unread,
Feb 24, 2012, 12:37:27 PM2/24/12
to sqlal...@googlegroups.com
I wrote a small script to clean up the db of old entries. 

So when I do obj.delete() I get this error. I also do a session.update(obj) right before to make sure the object is present in the session. 

  File "deletor.py", line 95, in delete
    m.delete()
  File "/usr/lib/python2.5/site-packages/elixir/entity.py", line 963, in delete
    return object_session(self).delete(self, *args, **kwargs)
  File "/var/lib/python-support/python2.5/sqlalchemy/orm/session.py", line 961, in delete
    for c, m in _cascade_iterator('delete', instance):
  File "/var/lib/python-support/python2.5/sqlalchemy/orm/session.py", line 1253, in _cascade_iterator
    for (o, m) in mapper.cascade_iterator(cascade, instance._state, **kwargs):
  File "/var/lib/python-support/python2.5/sqlalchemy/orm/mapper.py", line 1335, in cascade_iterator
    instance, instance_mapper, corresponding_state  = iterator.next()
  File "/var/lib/python-support/python2.5/sqlalchemy/orm/properties.py", line 451, in cascade_iterator
    instances = attributes.get_as_list(state, self.key, passive=passive)
  File "/var/lib/python-support/python2.5/sqlalchemy/orm/attributes.py", line 1151, in get_as_list
    x = attr.get(state, passive=passive)
  File "/var/lib/python-support/python2.5/sqlalchemy/orm/attributes.py", line 279, in get
    value = callable_()
  File "/var/lib/python-support/python2.5/sqlalchemy/orm/strategies.py", line 466, in __call__
    result = q.all()
  File "/var/lib/python-support/python2.5/sqlalchemy/orm/query.py", line 878, in all
    return list(self)
  File "/var/lib/python-support/python2.5/sqlalchemy/orm/query.py", line 1017, in iterate_instances
    context.attributes.get(('populating_mapper', ii), _state_mapper(ii))._post_instance(context, ii)
  File "/var/lib/python-support/python2.5/sqlalchemy/orm/mapper.py", line 1517, in _post_instance
    p(state.obj(), **kwargs)
  File "/var/lib/python-support/python2.5/sqlalchemy/orm/mapper.py", line 1547, in post_execute
    self.populate_instance(selectcontext, instance, row, isnew=False, instancekey=identitykey, ispostselect=True, only_load_props=only_load_props)
  File "/var/lib/python-support/python2.5/sqlalchemy/orm/mapper.py", line 1484, in populate_instance
    (newpop, existingpop, post_proc) = selectcontext.exec_with_path(self, prop.key, prop.create_row_processor, selectcontext, self, row)
  File "/var/lib/python-support/python2.5/sqlalchemy/orm/query.py", line 1703, in exec_with_path
    return fn(*args, **kwargs)
  File "/var/lib/python-support/python2.5/sqlalchemy/orm/interfaces.py", line 532, in create_row_processor
    return self._get_context_strategy(selectcontext).create_row_processor(selectcontext, mapper, row)
  File "/var/lib/python-support/python2.5/sqlalchemy/orm/strategies.py", line 75, in create_row_processor
    elif self.columns[0] in row:
TypeError: argument of type 'NoneType' is not iterable


Can I get a hint to what might be wrong? 

Michael Bayer

unread,
Feb 24, 2012, 4:04:22 PM2/24/12
to sqlal...@googlegroups.com


I can give you a hint if you give us one .... a short, self-contained and runnable test script illustrating how you're getting it to do this ?


Jakob D.

unread,
Feb 27, 2012, 8:07:43 AM2/27/12
to sqlal...@googlegroups.com
Sure, I thought that might be something obvious.

Here's runnable test which gives the above error:

import elixir
from elixir import *
from elixir.events import *
from sqlalchemy.orm import scoped_session, sessionmaker
from smisk.config import config

class Project(Entity):
using_options(tablename='projects')
id                  = Field(String(100), primary_key=True)
media              = OneToMany('Media', cascade='all, delete')

class Media(Entity):
using_options(tablename='media', inheritance='multi')
id = Field(String(50), primary_key=True)
project = ManyToOne('Project')
variants        = OneToMany('MediaVariant', cascade='all, delete')
attributes      = OneToMany('MediaAttribute', cascade='all, delete')
status = Field(String(20), nullable=True)

class MediaAttribute(Entity):
using_options(tablename='media_attributes')
media = ManyToOne('Media', primary_key=True)
attr_key = Field(String(100), primary_key=True)
attr_value = Field(Unicode(255))

class MediaVariant(Entity):
using_options(tablename='media_variants', inheritance='multi')
id         = Field(Integer, primary_key=True, autoincrement=True)
original   = ManyToOne('Media')

class Image(Media):
using_options(tablename='images', inheritance='multi')
width = Field(Integer, nullable=False, default=0)
height = Field(Integer, nullable=False, default=0)
class ImageVariant(MediaVariant):
using_options(tablename='image_variants', inheritance='multi')
width = Field(Integer, nullable=False, default=0)
height = Field(Integer, nullable=False, default=0)
elixir.session = scoped_session(sessionmaker(autoflush=False, transactional=True))
metadata.bind = get_db_url()
metadata.bind.recycle = 14400
setup_all(True)

media = Media.query.filter(Media.status == u'Deleted').filter_by(project_id=u'pf2u32e').all()

for m in media:
m.delete()
elixir.session.commit()

Michael Bayer

unread,
Feb 27, 2012, 9:53:43 AM2/27/12
to sqlal...@googlegroups.com
To make this script run, I had to remove "from smisk.config" since I don't have access to that, and also replace "get_db_url()" with a plain SQLite URL.   The script also doesn't include any data so I added a row that matches the particular delete you're doing.

The line that redefines the elixir session doesn't appear to work properly (keep in mind I don't work with Elixir), as Elixir seems to grab onto elixir.session separately before you change it's definition here.    The scoped_session() being made here doesn't appear to be used, so I changed it to:

elixir.session().autoflush=False
metadata.bind = create_engine("sqlite://", echo=True)
metadata.bind.recycle = 14400  # not sure what this is for, there's no "recycle" param on engine
setup_all(True)

m1 = Media(id="someid", status="Deleted", project_id=u'pf2u32e')
elixir.session.commit()
elixir.session.close()

media = Media.query.filter(Media.status == u'Deleted').filter_by(project_id=u'pf2u32e').all()

for m in media:
    m.delete()

session.commit()


the output is then what's expected, starting after the table creates, below.    Can you modify the attached script and ensure it reproduces your error ?  This is SQLAlchemy 0.7.5.

2012-02-27 09:47:00,092 INFO sqlalchemy.engine.base.Engine BEGIN (implicit)
2012-02-27 09:47:00,093 INFO sqlalchemy.engine.base.Engine INSERT INTO media (id, project_id, status, row_type) VALUES (?, ?, ?, ?)
2012-02-27 09:47:00,093 INFO sqlalchemy.engine.base.Engine ('someid', u'pf2u32e', 'Deleted', 'media')
2012-02-27 09:47:00,093 INFO sqlalchemy.engine.base.Engine COMMIT
2012-02-27 09:47:00,094 INFO sqlalchemy.engine.base.Engine BEGIN (implicit)
2012-02-27 09:47:00,094 INFO sqlalchemy.engine.base.Engine SELECT media.id AS media_id, media.project_id AS media_project_id, media.status AS media_status, media.row_type AS media_row_type 
FROM media 
WHERE media.status = ? AND media.project_id = ?
2012-02-27 09:47:00,094 INFO sqlalchemy.engine.base.Engine (u'Deleted', u'pf2u32e')
2012-02-27 09:47:00,097 INFO sqlalchemy.engine.base.Engine SELECT media_variants.id AS media_variants_id, media_variants.original_id AS media_variants_original_id, media_variants.row_type AS media_variants_row_type 
FROM media_variants 
WHERE media_variants.original_id = ?
2012-02-27 09:47:00,097 INFO sqlalchemy.engine.base.Engine (u'someid',)
2012-02-27 09:47:00,098 INFO sqlalchemy.engine.base.Engine SELECT media_attributes.media_id AS media_attributes_media_id, media_attributes.attr_key AS media_attributes_attr_key, media_attributes.attr_value AS media_attributes_attr_value 
FROM media_attributes 
WHERE media_attributes.media_id = ?
2012-02-27 09:47:00,098 INFO sqlalchemy.engine.base.Engine (u'someid',)
2012-02-27 09:47:00,099 INFO sqlalchemy.engine.base.Engine DELETE FROM media WHERE media.id = ?
2012-02-27 09:47:00,099 INFO sqlalchemy.engine.base.Engine (u'someid',)
2012-02-27 09:47:00,099 INFO sqlalchemy.engine.base.Engine COMMIT

test.py

Jakob D.

unread,
Feb 27, 2012, 10:42:28 AM2/27/12
to sqlal...@googlegroups.com
Yes, the error was reproduced.

I'm using 0.4.7. 

I couldn't import Session, any particular reason you imported it?

Here's the output, it seems to get the entry just fine: (I only get one entry by id to minimize the output)

2012-02-27 16:19:37,080 INFO sqlalchemy.engine.base.Engine.0x..cL SHOW VARIABLES LIKE 'sql_mode'
2012-02-27 16:19:37,080 INFO sqlalchemy.engine.base.Engine.0x..cL {'charset': None}
2012-02-27 16:19:37,081 INFO sqlalchemy.engine.base.Engine.0x..cL DESCRIBE `projects`
2012-02-27 16:19:37,081 INFO sqlalchemy.engine.base.Engine.0x..cL {}
2012-02-27 16:19:37,083 INFO sqlalchemy.engine.base.Engine.0x..cL DESCRIBE `media`
2012-02-27 16:19:37,083 INFO sqlalchemy.engine.base.Engine.0x..cL {}
2012-02-27 16:19:37,084 INFO sqlalchemy.engine.base.Engine.0x..cL DESCRIBE `media_variants`
2012-02-27 16:19:37,085 INFO sqlalchemy.engine.base.Engine.0x..cL {}
2012-02-27 16:19:37,086 INFO sqlalchemy.engine.base.Engine.0x..cL DESCRIBE `image_variants`
2012-02-27 16:19:37,086 INFO sqlalchemy.engine.base.Engine.0x..cL {}
2012-02-27 16:19:37,087 INFO sqlalchemy.engine.base.Engine.0x..cL DESCRIBE `media_attributes`
2012-02-27 16:19:37,087 INFO sqlalchemy.engine.base.Engine.0x..cL {}
2012-02-27 16:19:37,088 INFO sqlalchemy.engine.base.Engine.0x..cL DESCRIBE `images`
2012-02-27 16:19:37,088 INFO sqlalchemy.engine.base.Engine.0x..cL {}
2012-02-27 16:19:37,096 INFO sqlalchemy.engine.base.Engine.0x..cL BEGIN
2012-02-27 16:19:37,096 INFO sqlalchemy.engine.base.Engine.0x..cL SELECT media.id AS media_id, media.project_id AS media_project_id, media.status AS media_status, media.row_type AS media_row_type 
FROM media 
WHERE media.id = %s ORDER BY media.id 
 LIMIT 0, 1
2012-02-27 16:19:37,096 INFO sqlalchemy.engine.base.Engine.0x..cL [u'02pMhPqJkOQYm27sW0NAzxGoarN']
2012-02-27 16:19:37,098 INFO sqlalchemy.engine.base.Engine.0x..cL SELECT images.media_id AS images_media_id, images.width AS images_width, images.height AS images_height 
FROM images 
WHERE %s = images.media_id
2012-02-27 16:19:37,098 INFO sqlalchemy.engine.base.Engine.0x..cL ['02pMhPqJkOQYm27sW0NAzxGoarN']
2012-02-27 16:19:37,100 INFO sqlalchemy.engine.base.Engine.0x..cL SELECT media_variants.id AS media_variants_id, media_variants.original_id AS media_variants_original_id, media_variants.row_type AS media_variants_row_type 
FROM media_variants 
WHERE media_variants.original_id = %s ORDER BY media_variants.id
2012-02-27 16:19:37,101 INFO sqlalchemy.engine.base.Engine.0x..cL ['02pMhPqJkOQYm27sW0NAzxGoarN']
2012-02-27 16:19:37,103 INFO sqlalchemy.engine.base.Engine.0x..cL SELECT image_variants.mediavariant_id AS image_variants_mediavariant_id, image_variants.width AS image_variants_width, image_variants.height AS image_variants_height 
FROM image_variants 
WHERE %s = image_variants.mediavariant_id
2012-02-27 16:19:37,103 INFO sqlalchemy.engine.base.Engine.0x..cL [316956L]
Traceback (most recent call last):
  File "delete_test2.py", line 63, in <module>
    media.delete()

Michael Bayer

unread,
Feb 27, 2012, 11:12:57 AM2/27/12
to sqlal...@googlegroups.com
On Feb 27, 2012, at 10:42 AM, Jakob D. wrote:

Yes, the error was reproduced.

I'm using 0.4.7. 


ouch !



I couldn't import Session, any particular reason you imported it?

i was trying to play with how to get elixir.session to mean something.   It's not something that would work in 0.4.



Here's the output, it seems to get the entry just fine: (I only get one entry by id to minimize the output)

The current version of Elixir doesn't seem to even work with 0.4.7 as it's calling upon session.add().   Nor does the current version of SQLite as there was a change in their cursor behavior that was long ago accounted for in SQLA, so I switched to MySQL.    But with 0.4.7p1 where add() was added, it runs fine, as with 0.4.8.

Perhaps Elixir version here ?




--
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/-/LhpRDeCqYNYJ.
To post to this group, send email to sqlal...@googlegroups.com.
To unsubscribe from this group, send email to sqlalchemy+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.

Message has been deleted

Jakob D.

unread,
Feb 27, 2012, 11:48:24 AM2/27/12
to sqlal...@googlegroups.com
I have an old version of elixir as well...

Something seems to have gone wrong with some entries, I try with different entries and it works for most of them, but sometimes I'll get this error. I have no idea what the difference could be though, they look exactly the same as the working ones when looking in the db. 

However, I thought that maybe it's better to just execute custom sql here, since there are so many. Then maybe I don't have to populate the session with 900 objects, but instead delete them without retrieving them.

Something like: session.execute('Delete from...')

And then maybe I wont get this error since we never "Iterate objects".
Reply all
Reply to author
Forward
0 new messages