Reputation: 1591
I am trying to update an sqlalchemy database. It throws an error based off the string I try to implement into the query. I dont want to hardcode the link, but rather use the variable as I am looping through my database.
print (u)
(u'http://32rfckwuorlf4dlv.ca/linklist/1-general\n',)
currentlink = (str(u)[:-3][1:])
print currentlink
u'http://32rfckwuorlf4dlv.ca/linklist/1-general\n'
This fails..
x = Item.query.filter_by(link=currentlink).first()
print x
try:
print x.id
x.title = 'test'
except Exception as e:
print(str(e))
db.session.commit()
prints :'NoneType' object has no attribute 'id'
This works..
x = Item.query.filter_by(link=u'http://32rfckwuorlf4dlv.ca/linklist/1-general\n').first()
print x
try:
print x.id
x.title = 'test'
except Exception as e:
print(str(e))
db.session.commit()
prints: http://32rfckwuorlf4dlv.ca/linklist/1-general\n'> prints: 90
Upvotes: 0
Views: 275
Reputation: 20508
Okay the reason is that currentlink
is actually "u'http://32rfckwuorlf4dlv.ca/linklist/1-general\\n"
, not u'http://32rfckwuorlf4dlv.ca/linklist/1-general\n'
.
Assuming u
is a tuple
, you should do
currentlink = u[0]
instead.
If you want to make your example work (not recommended by the way, it's only for edification purposes):
currentlink = str(u)[:-3][3:].replace("\\n", "\n")
If u
is a str
instead, you need to figure out why it's a str
instead of a tuple
.
Upvotes: 1
Reputation: 22
two things
seems the variable u
in your description is a tuple, so you can just use currentlink = u[0]
to get the url link.
The currentlink u'http://32rfckwuorlf4dlv.ca/linklist/1-general\n' and http://32rfckwuorlf4dlv.ca/client-check\n in hardcode are different. And I think this is the reason that you get a Nonetype from database.
Upvotes: 1