问题
I should first mention that I'm using SqlAlchemy through Flask-SqlAlchemy. I don't believe this affects the issue but if it does, please let me know.
Here is the relevant part of the error message I'm getting when running the create_all function in SqlAlchemy
InterfaceError: (InterfaceError) Error binding parameter 4 - probably unsupported type. u'INSERT INTO podcasts (feed_url, title, url, last_updated, feed_data) VALUES (?, ?, ?, ?, ?)' (u'http://example.com/feed', u'Podcast Show Title', u'http://example.com', '2012-04-17 20:28:49.117000'
Here is my model:
class Podcast(db.Model):
import datetime
__tablename__ = 'podcasts'
id = db.Column(db.Integer, primary_key=True)
feed_url = db.Column(db.String(150), unique=True)
title = db.Column(db.String(200))
url = db.Column(db.String(150))
last_updated = db.Column(db.DateTime, default=datetime.datetime.now)
feed_data = db.Column(db.Text)
def __init__(self, feed_url):
import feedparser
self.feed_url = feed_url
self.feed_data = feedparser.parse(self.feed_url)
self.title = self.feed_data['feed']['title']
self.url = self.feed_data['feed']['link']
Can someone tell me how I can get this to work? I've also tried the following model but that also doesn't work. Same error.
class Podcast(db.Model):
import datetime
__tablename__ = 'podcasts'
id = db.Column(db.Integer, primary_key=True)
feed_url = db.Column(db.String(150), unique=True)
title = db.Column(db.String(200))
url = db.Column(db.String(150))
last_updated = db.Column(db.DateTime)
feed_data = db.Column(db.Text)
def __init__(self, feed_url):
import feedparser
self.feed_url = feed_url
self.feed_data = feedparser.parse(self.feed_url)
self.last_updated = datetime.datetime.now()
self.title = self.feed_data['feed']['title']
self.url = self.feed_data['feed']['link']
回答1:
Try to use datetime.datetime.utcnow()
. This works for me.
回答2:
last_updated = db.Column(db.DateTime, default=db.func.current_timestamp())
I think this will work
回答3:
Set the import out the class.
from datetime import datetime
last_updated = db.Column(db.DateTime, default=datetime.now())
or
import datetime
last_updated = db.Column(db.DateTime, default=datetime.datetime.now())
I did this
回答4:
Look at your line:
last_updated = db.Column(db.DateTime, default=datetime.datetime.now)
There is no datetime.datetime.now
attribute in python. However, there is a datetime.datetime.now()
function in python. You were just missing a pair of parentheses.
来源:https://stackoverflow.com/questions/10201648/sqlalchemy-wont-accept-datetime-datetime-now-value-in-a-datetime-column