Logicman
Logicman

Reputation: 129

SQLAlchemy: Error binding parameter 0 - probably unsupported type

I am trying to create an instance of a table with the following code:

c = 'dcoh92j'
new_comment = Comment(rcomment = c, rtime = datetime.datetime.now())

But I receive this error:

sqlalchemy.exc.InterfaceError: (sqlite3.InterfaceError) Error binding parameter 0 - probably unsupported type. [SQL: 'INSERT INTO comments (rcomment, rtime) VALUES (?, ?)'] [parameters: (Comment(id='dcoh92j'), datetime.datetime(2017, 1, 20, 12, 38, 38, 836433))

Here is my table schema:

class Comment(Base):
    __tablename__ = 'comments'
    id = Column(Integer, Sequence('comment_id_seq'), primary_key=True)
    rcomment = Column(String)
    rtime = Column(String)

    def __repr__(self):
        return "<Comment(rcomment='{0}', rtime='(1)')>".format(self.rcomment, self.rtime)
  1. Why does SQLAlchemy think the parameters I'm trying to insert into the database are (Comment(id='dcoh92j'), datetime.datetime(2017, 1, 20, 12, 38, 38, 836433)? Instead of just 'dcoh92j' and 2017, 1, 20, 12, 38, 38, 836433
  2. Where is this Comment(id='dcoh92j') coming from?

Full code here.

Upvotes: 4

Views: 9054

Answers (2)

Logicman
Logicman

Reputation: 129

Okay I ended up figuring it out. Of course after 3 hours of bug testing and ripping out my hair, it was a simple matter of wrapping c and datetime.datetime.now() in str() tags.

Fix:

new_comment = Comment(rcomment = str(c), rtime = str(datetime.datetime.now()))

Upvotes: 4

metmirr
metmirr

Reputation: 4302

You define rtime = Column(String) as a string field but datetime.datetime.utcnow() is a datetime object. So instead of String field use DateTime:

from sqlalchemy import DateTime
rtime = Column(DateTime)

or convert datetime object to a string:

str_rtime = str(datetime.datetime.utcnow())
new_comment = Comment(rcomment=c, rtime=str_rtime)

Upvotes: 1

Related Questions