私は2つのモデルを持っています:Play
とPlayParticipant
は、(部分的に) 次のように定義されています:
class PlayParticipant(models.Model):
player = models.ForeignKey('Player')
play = models.ForeignKey('Play')
note = models.CharField(max_length=100, blank=True)
コードの一部にp
id 8581 の play があり、それに参加者を追加したいと考えています。私は.create()
それを行うために RelatedManager を使用しようとしています:
p.playparticipant_set.create(player_id=2383)
そこから、Django は以下を構築します。
INSERT INTO `api_playparticipant` (`player_id`, `play_id`, `note`) VALUES (2383, 2383, '')
これは Django のバグですか、それとも誤用.create()
ですか?
サニティチェック用のコピーペーストシェル:
In [17]: p = Play.objects.get(id=8581)
In [18]: p.id
Out[18]: 8581L
In [19]: p.playparticipant_set.create(player_id=2383)
...
IntegrityError: (1452, 'Cannot add or update a child row: a foreign key constraint fails (`gc/api_playparticipant`, CONSTRAINT `play_id_refs_id_60804ffd462e0029` FOREIGN KEY (`play_id`) REFERENCES `api_play` (`id`))')
query.log から:
5572 Query INSERT INTO `api_playparticipant` (`player_id`, `play_id`, `note`) VALUES (2383, 2383, '')