Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SqlAlchemy won't accept datetime.datetime.now value in a DateTime column

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']
like image 238
Kylee Avatar asked Apr 18 '12 01:04

Kylee


3 Answers

Try to use datetime.datetime.utcnow(). This works for me.

like image 194
Bijan Avatar answered Oct 10 '22 21:10

Bijan


last_updated = db.Column(db.DateTime, default=db.func.current_timestamp())

I think this will work.

like image 22
Savad KP Avatar answered Oct 10 '22 20:10

Savad KP


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

like image 30
Leonardo Ramos Avatar answered Oct 10 '22 22:10

Leonardo Ramos