How to get record by url, insert if not found
I'm doing this currently and getting an error:
Attribute Error 'Table' object has not attribute url
p = session.query(pages.url == someurl).first()
if p is None:
p = Page()
p....
..
session.add(p)
session.commit()
SomeFu开发者_如何转开发nction(p)
I am weary if something returns null my code is going to fail, is it correct?
No, your approach is not correct, and the error is not of the 'no row' nature. From the error message, 'pages' is obviously a Table object, and not a class mapped to a table. Either you change the expression to Pages.url==someurl
or you use pages.c.url==someurl
, thus, specifying that you are looking for a (c) column by the name of url. Still, it is very unclear what you are trying to achieve here.
精彩评论