Thread: [SQLObject] A problem about "ForeignKey", help!!
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
From: Hanjie <luc...@gm...> - 2009-04-09 21:45:29
|
Hi any man: please help me, I have been confused by this problem for whole day. I have two table and I want to use a foreignKey connect then together. My coding is following: class postTable(SQLObject): title = UnicodeCol(length=50) content = UnicodeCol() comments=MultipleJoin('commentTable') class commentTable(SQLObject): post=ForeignKey('postTable') authorName=UnicodeCol(length=255,notNull=False) content=UnicodeCol() Then I input "describe table post_table" find the structure of post_table only content : id, title,content. I can use like q.id, q.title, q.content. But, when I want to use q.comment, the error arise: I waste a whole day on this issue, please help me, I really need to use q.comment. how to solve it. Thank you in advance My problem detail(I am studying how to use turbogears): ---------------------------model.py--------------------------------- class postTable(SQLObject): title = UnicodeCol(length=50) content = UnicodeCol() postDate = DateTimeCol(default=datetime.now()) isPublished= BoolCol(default=False) comments=MultipleJoin('commentTable') def _get_html_content(self): return publish_parts(self.content, writer_name="html")["html_body"] html_content=property(_get_html_content) class commentTable(SQLObject): post=ForeignKey('postTable') authorName=UnicodeCol(length=255,notNull=False) authorEmail=StringCol(length=255,notNull=False) authorUrl=StringCol(length=255,notNull=False) commentDate=DateTimeCol(default=datetime.now()) content=UnicodeCol() ------------------------------control.py------------------------------------------- class Root(controllers.RootController): @expose(template="assignment.templates.welcome") def index(self): posts = assignment.model.postTable.select() return dict(posts=posts) @expose(template="assignment.templates.post") def post(self,id): p=assignment.model.postTable.get(int(id)) return dict(post=p) ----------------------------------welcome.kid---------------------------------------- <body> <div id = "posts"> <div py:for="post in posts" id="post_${post.id}"> <h2 py:content="post.title">Post title here</h2> <div class="postmeta"> Posted on <span class="postdate" py:content="post.postDate">01/01/01</span> </div> <div class="content" py:content="XML(post.html_content)"> This is where your post's content is displayed. </div> <div class="comments"> <div py:for="comment in post.comments" id="comment_${ comment.id}"> ##problem should be here <div class="commentmeta"> Comment by <span py:if="comment.authorUrl" py:strip=''> <a href="${comment.authorUrl}" py:content="comment.authorName">Author with link</a> </span> <span py:if="not comment.authorUrl" py:strip='' py:content="comment.authorName"> Author without link </span> on <span py:content="comment.commentDate">01/01/01</span> </div> </div> </div> <div class="content" py:content="comment.content"> Comment content here. </div> </div> </div> </body> --------------------------------------------trac------------------------------------------------- Page handler: <bound method Root.index of <assignment.controllers.Root object at 0x019E90F0>> Traceback (most recent call last): File "c:\python25\lib\site-packages\cherrypy-2.3.0-py2.5.egg\cherrypy\_cphttptools.py", line 121, in _run self.main() File "c:\python25\lib\site-packages\cherrypy-2.3.0-py2.5.egg\cherrypy\_cphttptools.py", line 264, in main body = page_handler(*virtual_path, **self.params) File "<string>", line 3, in index File "c:\python25\lib\site-packages\TurboGears-1.0.8-py2.5.egg\turbogears\controllers.py", line 360, in expose *args, **kw) File "<string>", line 5, in run_with_transaction File "c:\python25\lib\site-packages\TurboGears-1.0.8-py2.5.egg\turbogears\database.py", line 359, in so_rwt retval = func(*args, **kw) File "<string>", line 5, in _expose File "c:\python25\lib\site-packages\TurboGears-1.0.8-py2.5.egg\turbogears\controllers.py", line 373, in <lambda> mapping, fragment, args, kw))) File "c:\python25\lib\site-packages\TurboGears-1.0.8-py2.5.egg\turbogears\controllers.py", line 423, in _execute_func return _process_output(output, template, format, content_type, mapping, fragment) File "c:\python25\lib\site-packages\TurboGears-1.0.8-py2.5.egg\turbogears\controllers.py", line 88, in _process_output fragment=fragment) File "c:\python25\lib\site-packages\TurboGears-1.0.8-py2.5.egg\turbogears\view\base.py", line 159, in render return engine.render(**kw) File "c:\python25\lib\site-packages\TurboKid-1.0.4-py2.5.egg\turbokid\kidsupport.py", line 206, in render output=output, format=format) File "c:\python25\lib\site-packages\kid-0.9.6-py2.5.egg\kid\__init__.py", line 301, in serialize raise_template_error(module=self.__module__) File "c:\python25\lib\site-packages\kid-0.9.6-py2.5.egg\kid\__init__.py", line 299, in serialize return serializer.serialize(self, encoding, fragment, format) File "c:\python25\lib\site-packages\kid-0.9.6-py2.5.egg\kid\serialization.py", line 107, in serialize text = ''.join(self.generate(stream, encoding, fragment, format)) File "c:\python25\lib\site-packages\kid-0.9.6-py2.5.egg\kid\serialization.py", line 629, in generate for ev, item in self.apply_filters(stream, format): File "c:\python25\lib\site-packages\kid-0.9.6-py2.5.egg\kid\serialization.py", line 165, in format_stream for ev, item in stream: File "c:\python25\lib\site-packages\kid-0.9.6-py2.5.egg\kid\parser.py", line 221, in _coalesce for ev, item in stream: File "c:\python25\lib\site-packages\kid-0.9.6-py2.5.egg\kid\serialization.py", line 477, in inject_meta_tags for ev, item in stream: File "c:\python25\lib\site-packages\kid-0.9.6-py2.5.egg\kid\parser.py", line 179, in _track for p in stream: File "c:\python25\lib\site-packages\kid-0.9.6-py2.5.egg\kid\filter.py", line 32, in apply_matches item = stream.expand() File "c:\python25\lib\site-packages\kid-0.9.6-py2.5.egg\kid\parser.py", line 108, in expand for ev, item in self._iter: File "c:\python25\lib\site-packages\kid-0.9.6-py2.5.egg\kid\parser.py", line 179, in _track for p in stream: File "c:\python25\lib\site-packages\kid-0.9.6-py2.5.egg\kid\parser.py", line 221, in _coalesce for ev, item in stream: File "C:\Documents and Settings\hanjie\assignment\assignment\templates\welcome.py", line 109, in _pull File "<string>", line 1, in <lambda> File "c:\python25\lib\site-packages\SQLObject-0.10.4-py2.5.egg\sqlobject\joins.py", line 144, in performJoin inst.id) File "c:\python25\lib\site-packages\SQLObject-0.10.4-py2.5.egg\sqlobject\dbconnection.py", line 547, in _SO_selectJoin self.sqlrepr(value))) File "c:\python25\lib\site-packages\SQLObject-0.10.4-py2.5.egg\sqlobject\dbconnection.py", line 696, in queryAll return self._dbConnection._queryAll(self._connection, s) File "c:\python25\lib\site-packages\SQLObject-0.10.4-py2.5.egg\sqlobject\dbconnection.py", line 353, in _queryAll self._executeRetry(conn, c, s) File "c:\python25\lib\site-packages\SQLObject-0.10.4-py2.5.egg\sqlobject\mysql\mysqlconnection.py", line 117, in _executeRetry raise OperationalError(ErrorMessage(e)) OperationalError: Unknown column 'post_table_id' in 'where clause' Error location in template file 'C:\\Documents and Settings\\hanjie\\assignment\\assignment\\templates\\welcome.kid' between line 19, column 3 and line 20, column 4: <div class="comments"> |
From: Oleg B. <ph...@ph...> - 2009-04-09 23:17:49
|
On Thu, Apr 09, 2009 at 11:44:52PM +0200, Hanjie wrote: > class postTable(SQLObject): > title = UnicodeCol(length=50) > content = UnicodeCol() > comments=MultipleJoin('commentTable') > > class commentTable(SQLObject): > post=ForeignKey('postTable') > authorName=UnicodeCol(length=255,notNull=False) > content=UnicodeCol() > > But, when I want to use q.comment, the error arise: > OperationalError: Unknown column 'post_table_id' in 'where clause' The problem lies here: class postTable(SQLObject): comments=MultipleJoin('commentTable') class commentTable(SQLObject): post=ForeignKey('postTable') There are simply too many different names: 'comments', 'commentTable', 'post' and 'postTable'. Multitude of these names confuses SQLObject so it doesn't know how to count comments of a post. One can help SQLObject by explicitly naming the column in the other table: class postTable(SQLObject): comments=MultipleJoin('commentTable', joinColumn='post_id') Oleg. -- Oleg Broytmann http://phd.pp.ru/ ph...@ph... Programmers don't die, they just GOSUB without RETURN. |
From: Hanjie <luc...@gm...> - 2009-04-09 23:39:29
|
Great!!! eventually, i find the problem!!!! ForeignKey should use the same name with the related table. How simple it is, but I waste a whole day on it! Worry: post=ForeignKey('postTable') correct: postTable=ForeignKey('postTable') On Fri, Apr 10, 2009 at 1:17 AM, Oleg Broytmann <ph...@ph...> wrote: > On Thu, Apr 09, 2009 at 11:44:52PM +0200, Hanjie wrote: > > class postTable(SQLObject): > > title = UnicodeCol(length=50) > > content = UnicodeCol() > > comments=MultipleJoin('commentTable') > > > > class commentTable(SQLObject): > > post=ForeignKey('postTable') > > authorName=UnicodeCol(length=255,notNull=False) > > content=UnicodeCol() > > > > But, when I want to use q.comment, the error arise: > > OperationalError: Unknown column 'post_table_id' in 'where clause' > > The problem lies here: > > class postTable(SQLObject): > comments=MultipleJoin('commentTable') > > class commentTable(SQLObject): > post=ForeignKey('postTable') > > There are simply too many different names: 'comments', 'commentTable', > 'post' and 'postTable'. Multitude of these names confuses SQLObject so it > doesn't know how to count comments of a post. One can help SQLObject by > explicitly naming the column in the other table: > > class postTable(SQLObject): > comments=MultipleJoin('commentTable', joinColumn='post_id') > > Oleg. > -- > Oleg Broytmann http://phd.pp.ru/ ph...@ph... > Programmers don't die, they just GOSUB without RETURN. > > > ------------------------------------------------------------------------------ > This SF.net email is sponsored by: > High Quality Requirements in a Collaborative Environment. > Download a free trial of Rational Requirements Composer Now! > http://p.sf.net/sfu/www-ibm-com > _______________________________________________ > sqlobject-discuss mailing list > sql...@li... > https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss > |