[sqlalchemy] AttributeError: 'NoneType' object has no attribute 'groups'

2011-02-09 Thread dalia
Hi,

I'm very new to sql alchemy..want help in debugging my code. I'm
writing a web application using pylons, formalchemy and sqlalchemy.
The issue is as follows -

I've 2 tables namely source and pstn. i've declared them as follows -

class Source(Base):
__tablename__ = 'source'
id = Column(Integer, primary_key=True)
sh_code = Column(Integer, nullable=False)
pstn_no = Column(Integer)
details = Column(Text(), nullable=False)
active = Column(Boolean, default=True)

class PSTN(Base):
__tablename__ = 'pstn'
id = Column(Integer, primary_key=True)
prev_no = Column(Integer)
latest_no = Column(Integer, nullable=False)
last_con = Column(Date())
expiry_code = Column(Text())
active = Column(Boolean, default=True)

now i've joined the two tables to create a view namely History like
this -

pstn_table = PSTN.__table__
source_table = Source.__table__

j = join(pstn_table, source_table, pstn_table.c.latest_no ==
source_table.c.pstn_no)

class History(Base):
__table__ = j

in my admincontroller, i've set up the code so that whenever a new
entry is added to 'source', the same 'pstn_no' would be inserted to
'pstn' table's 'latest_no' column. This piece is working fine and
whenever i try to access class History from the grid, it loads all
fine. The underlying sql query for the join is like this -

 SELECT pstn.id AS pstn_id, source.id AS source_id, pstn.prev_no AS
pstn_prev_no, pstn.latest_no AS pstn_latest_no, pstn.last_con AS
pstn_last_con, pstn.expiry_code AS pstn_expiry_code, pstn.active AS
pstn_active, source.active AS source_active, source.sh_code AS
source_sh_code, source.pstn_no AS source_pstn_no, source.details AS
source_details
FROM pstn JOIN source ON pstn.latest_no = source.pstn_no
WHERE pstn.active = ?
 LIMIT 3 OFFSET 0
10:05:13,089 INFO  [sqlalchemy.engine.base.Engine.0x...f490] (True,)

Now, the problem is, if I manually add some entry into the 'pstn' and
'source' table where source.pstn_no = pstn.latest_no and try to access
the History grid, the underlying sql remains the same but on the
webpage, i get this error -

⇝ AttributeError: 'NoneType' object has no attribute 'groups'
View as:   Interactive (full)  |  Text (full)  |  XML (full)
clear this
clear this
Module testapp.lib.admincontroller:292 in index view
  def index(self, format='html', **kwargs):
   REST api
   page = self.get_page()
   fs = self.get_grid()
   fs = fs.bind(instances=page)
  page = self.get_page()
Module testapp.controllers.admin:45 in get_page view
options = dict(page=int(request.params.get('page', '1')))
 return Page(query,options)

   def hide(self, id):
  return Page(query,options)
Module webhelpers.paginate:434 in __init__ view
  self.items = self.collection
   else:
   self.items =
list(self.collection[self.first_item-1:self.last_item])

   # Links to previous and next page
  self.items = list(self.collection[self.first_item-1:self.last_item])
Module webhelpers.paginate:263 in __getitem__ view
  if not isinstance(range, slice):
   raise Exception, __getitem__ without slicing not
supported
   return self.obj[range]

   def __len__(self):
  return self.obj[range]
Module sqlalchemy.orm.query:1528 in __getitem__ view
  return list(res)[None:None:item.step]
   else:
   return list(res)
   else:
   return list(self[item:item+1])[0]
  return list(res)
Module sqlalchemy.orm.query:1797 in instances view
  process[0](row, rows)
   elif single_entity:
   rows = [process[0](row, None) for row in fetch]
   else:
   rows = [util.NamedTuple([proc(row, None) for
proc in process],
  rows = [process[0](row, None) for row in fetch]
Module sqlalchemy.orm.mapper:2275 in _instance view
  instancekey=identitykey, isnew=isnew)
is \
   EXT_CONTINUE:
   populate_state(state, dict_, row, isnew,
only_load_props)

   else:
  populate_state(state, dict_, row, isnew, only_load_props)
Module sqlalchemy.orm.mapper:2153 in populate_state view
  for key, populator in populators:
   populator(state, dict_, row)

   session_identity_map = context.session.identity_map
  populator(state, dict_, row)
Module sqlalchemy.orm.strategies:129 in new_execute view
  if col is not None and col in row:
   def new_execute(state, dict_, row):
   dict_[key] = row[col]
   return new_execute, None, None
   else:
  dict_[key] = row[col]
Module sqlalchemy.engine.base:2023 in 

Re: [sqlalchemy] AttributeError: 'NoneType' object has no attribute 'groups'

2011-02-09 Thread Michael Bayer
It seems like your issue is that a query is executing and you are expecting a 
certain result, but you are not getting it.   So there's not a lot of relevant 
detail here for someone that doesn't have your application open in front of 
them (test data, exact code that produces query, illustration of expected 
result row not present, extraneous stack traces regarding pagination logic and 
secondary side effects like 'NoneType has no attribute groups'  removed).   
So on your end, turn on echo ='debug', ensure the expected rows are returned 
from the statement as it executes, and keep in mind an ORM query against a 
single entity, i.e. session.query(Source).filter(...), will only return a 
Source object if a primary key for Source is fully present in the row.


On Feb 9, 2011, at 5:21 AM, dalia wrote:

 Hi,
 
 I'm very new to sql alchemy..want help in debugging my code. I'm
 writing a web application using pylons, formalchemy and sqlalchemy.
 The issue is as follows -
 
 I've 2 tables namely source and pstn. i've declared them as follows -
 
 class Source(Base):
__tablename__ = 'source'
id = Column(Integer, primary_key=True)
sh_code = Column(Integer, nullable=False)
pstn_no = Column(Integer)
details = Column(Text(), nullable=False)
active = Column(Boolean, default=True)
 
 class PSTN(Base):
__tablename__ = 'pstn'
id = Column(Integer, primary_key=True)
prev_no = Column(Integer)
latest_no = Column(Integer, nullable=False)
last_con = Column(Date())
expiry_code = Column(Text())
active = Column(Boolean, default=True)
 
 now i've joined the two tables to create a view namely History like
 this -
 
 pstn_table = PSTN.__table__
 source_table = Source.__table__
 
 j = join(pstn_table, source_table, pstn_table.c.latest_no ==
 source_table.c.pstn_no)
 
 class History(Base):
__table__ = j
 
 in my admincontroller, i've set up the code so that whenever a new
 entry is added to 'source', the same 'pstn_no' would be inserted to
 'pstn' table's 'latest_no' column. This piece is working fine and
 whenever i try to access class History from the grid, it loads all
 fine. The underlying sql query for the join is like this -
 
 SELECT pstn.id AS pstn_id, source.id AS source_id, pstn.prev_no AS
 pstn_prev_no, pstn.latest_no AS pstn_latest_no, pstn.last_con AS
 pstn_last_con, pstn.expiry_code AS pstn_expiry_code, pstn.active AS
 pstn_active, source.active AS source_active, source.sh_code AS
 source_sh_code, source.pstn_no AS source_pstn_no, source.details AS
 source_details
 FROM pstn JOIN source ON pstn.latest_no = source.pstn_no
 WHERE pstn.active = ?
 LIMIT 3 OFFSET 0
 10:05:13,089 INFO  [sqlalchemy.engine.base.Engine.0x...f490] (True,)
 
 Now, the problem is, if I manually add some entry into the 'pstn' and
 'source' table where source.pstn_no = pstn.latest_no and try to access
 the History grid, the underlying sql remains the same but on the
 webpage, i get this error -
 
 ⇝ AttributeError: 'NoneType' object has no attribute 'groups'
 View as:   Interactive (full)  |  Text (full)  |  XML (full)
 clear this
 clear this
 Module testapp.lib.admincontroller:292 in index view
   def index(self, format='html', **kwargs):
   REST api
   page = self.get_page()
   fs = self.get_grid()
   fs = fs.bind(instances=page)
 page = self.get_page()
 Module testapp.controllers.admin:45 in get_page view
 options = dict(page=int(request.params.get('page', '1')))
 return Page(query,options)
 
   def hide(self, id):
 return Page(query,options)
 Module webhelpers.paginate:434 in __init__ view
   self.items = self.collection
   else:
   self.items =
 list(self.collection[self.first_item-1:self.last_item])
 
   # Links to previous and next page
 self.items = list(self.collection[self.first_item-1:self.last_item])
 Module webhelpers.paginate:263 in __getitem__ view
   if not isinstance(range, slice):
   raise Exception, __getitem__ without slicing not
 supported
   return self.obj[range]
 
   def __len__(self):
 return self.obj[range]
 Module sqlalchemy.orm.query:1528 in __getitem__ view
   return list(res)[None:None:item.step]
   else:
   return list(res)
   else:
   return list(self[item:item+1])[0]
 return list(res)
 Module sqlalchemy.orm.query:1797 in instances view
   process[0](row, rows)
   elif single_entity:
   rows = [process[0](row, None) for row in fetch]
   else:
   rows = [util.NamedTuple([proc(row, None) for
 proc in process],
 rows = [process[0](row, None) for row in fetch]
 Module sqlalchemy.orm.mapper:2275 in _instance view