SQLAlchemy: Error Link parameter 0 - type probably not supported

advertisements

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.


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)