SQLAlchemy:不能适应类型' dict'

时间:2017-05-27 19:18:44

标签: flask sqlalchemy flask-sqlalchemy psycopg2

我有一个烧瓶应用程序,并尝试使用SQLAlchemy将对象存储在Postgres数据库中 我收到以下错误:

sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) can't adapt type
'dict' [SQL: 'INSERT INTO events (organizer_id, name, date, _address,
_polygon, starter_time, main_time, dessert_time, afterparty_flag,
afterparty_address, afterparty_description) VALUES (%(organizer_id)s, %
(name)s, %(date)s, %(_address)s, %(_polygon)s, %(starter_time)s, %
(main_time)s, %(dessert_time)s, %(afterparty_flag)s, %(afterparty_address)s, %
(afterparty_description)s) RETURNING events.id'] [parameters: {'organizer_id':
 7, 'name': 'testevent', 'date': datetime.datetime(2017, 5, 27, 19, 8, 38,
 90484), '_address': None, '_polygon': {}, 'starter_time':
datetime.datetime(2017, 5, 27, 19, 8, 38, 90500), 'main_time':
datetime.datetime(2017, 5, 27, 19, 8, 38, 90508), 'dessert_time':
datetime.datetime(2017, 5, 27, 19, 8, 38, 90514), 'afterparty_flag': True,
'afterparty_address': None, 'afterparty_description': None}]

我的活动模特:

class Event(db.Model):
__tablename__ = 'events'
id = db.Column(db.Integer, primary_key=True)
organizer_id = db.Column(db.Integer, db.ForeignKey('organizers.id'))

# general
name = db.Column(db.String(64))
date = db.Column(db.DateTime(), default=datetime.utcnow)
_address = db.Column(db.Text(), default={})
_polygon = db.Column(db.Text(), default={})

# timetable
starter_time = db.Column(db.DateTime(), default=datetime.utcnow)
main_time = db.Column(db.DateTime(), default=datetime.utcnow)
dessert_time = db.Column(db.DateTime(), default=datetime.utcnow)

# afterparty
afterparty_flag = db.Column(db.Boolean(), default=True)
afterparty_address = db.Column(db.String(140))
afterparty_description = db.Column(db.String(140))

postcode = None
city = None

我的数据库中有一个ID为7的组织者 我已经花了几个小时阅读日志和试验。

你知道怎么了?

1 个答案:

答案 0 :(得分:0)

更改以下两行修复了错误。

_address = db.Column(db.Text(), default={})
_polygon = db.Column(db.Text(), default={})


_address = db.Column(db.Text(), default="")
_polygon = db.Column(db.Text(), default="")