SQLAlchemy DateTime不接受datetime.datetime.utcnow()

时间:2017-06-16 09:18:12

标签: python sql sqlalchemy

我在提交post incto DB时遇到问题。这里我的sqlalchemy表代码。用户和帖子

class User(db.Model):
  id = db.Column(db.Integer, primary_key=True)
  username = db.Column(db.String(64), unique=True)
  password = db.Column(db.String(80))
  email = db.Column(db.String(80), unique=True)
  avatar = db.Column(db.String(80))
  posts = relationship('Post', backref='owner', lazy='dynamic')

class Post(db.Model):
  id = db.Column(db.Integer, primary_key=True)
  title = db.Column(db.String(128))
  body = db.Column(db.String(1024))
  date = db.Column(db.DateTime)
  author = db.Column(db.Integer, db.ForeignKey('user.id'))

我做:

from app import db, User, Post

制作与用户相关的用户和帖子

user = User(username='username', password='password', email='mail@mail.com', avatar='image.jpg')
post = Post(title='title', body='body text', date=datetime.datetime.utcnow(), author=user)

并提交

db.session.add(user)
db.session.commit()
db.session.add(post)
db.session.commit()

然后我总是得到同样的错误:

(sqlite3.InterfaceError) Error binding parameter 3 - probably unsupported type.
[SQL: 'INSERT INTO post (title, body, date, author) VALUES (?, ?, ?, ?)']
[parameters: ('title', 'body text', '2017-06-16 08:48:55.651632', <app.User object at 0x04C11770>)]

我尝试了很多不同的解决方案,但都没有。

0 个答案:

没有答案