我正在使用 Python Peewee ORM,我想知道如何通过简单地插入键而不是对象本身来插入外部字段。例如,我有一个名为 message 的模型,它的 message_type 是 ForeignKeyField。我知道 message_type 的键是 1,所以我试试这个:
>>> m = Message()
>>> m.message_type_id = 1
>>> m.text="aergaer"
>>> m.save()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "build/bdist.macosx-10.9-intel/egg/peewee.py", line 2479, in save
File "build/bdist.macosx-10.9-intel/egg/peewee.py", line 1775, in execute
File "build/bdist.macosx-10.9-intel/egg/peewee.py", line 1470, in _execute
File "build/bdist.macosx-10.9-intel/egg/peewee.py", line 1885, in execute_sql
File "build/bdist.macosx-10.9-intel/egg/peewee.py", line 1871, in sql_error_handler
sqlite3.IntegrityError: message.message_type_id may not be NULL
有人知道我该怎么做吗?