Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 281559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T05:12:48+00:00 2026-05-12T05:12:48+00:00

I have two models: Play and PlayParticipant , defined (in part) as: class PlayParticipant(models.Model):

  • 0

I have two models: Play and PlayParticipant, defined (in part) as:

class PlayParticipant(models.Model):
    player = models.ForeignKey('Player')
    play = models.ForeignKey('Play')
    note = models.CharField(max_length=100, blank=True)

A piece of my code has a play p which has id 8581, and I’d like to add participants to it. I’m trying to use RelatedManager’s .create() to do that, like:

p.playparticipant_set.create(player_id=2383)

From which, Django constructs:

INSERT INTO `api_playparticipant` (`player_id`, `play_id`, `note`) VALUES (2383, 2383, '')

Is this a bug in Django, or am I misusing .create()?

Copy-pasting shell for sanity check:

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`))')

From query.log:

5572 Query       INSERT INTO `api_playparticipant` (`player_id`, `play_id`, `note`) VALUES (2383, 2383, '')
  • 1 1 Answer
  • 3 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T05:12:48+00:00Added an answer on May 12, 2026 at 5:12 am

    I don’t see how is your first example supposed to be a bug? This behavior is completely intuitive. Your p is a play with an id of 2383, and you’re calling the create method on its related set. You’re also specifying an extra field, called player_id, and giving it a value of 2383. It’s only logical that both play id will be 2383 (because that’s the id of the play containing this related set) and that player id will also be 2383 (because you explicitly passed that value in).

    Your second example would seem to indicate a bug, but I cannot reproduce it. Here are my models:

    class Player(models.Model):
        name = models.CharField(max_length=100)
    
    class Play(models.Model):
        title = models.CharField(max_length=100)
    
    class PlayParticipant(models.Model):
        player = models.ForeignKey('Player')
        play = models.ForeignKey('Play')
        note = models.CharField(max_length=100, blank=True)
    

    Here’s the shell output:

    >>> player1 = Player.objects.create()
    >>> player2 = Player.objects.create()
    >>> player1.id
    2
    >>> player2.id
    3
    >>> play1 = Play.objects.create()
    >>> play2 = Play.objects.create()
    >>> play1.id
    3
    >>> play2.id
    4
    >>> pp1 = play1.playparticipant_set.create(player_id=2)
    >>> pp1.play.id
    3
    >>> pp1.player.id
    2
    

    In any case, why are you posting this to SO? Django has a bugtracker, a couple of active official mailing lists and a couple of IRC channels.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two models class Friend(models.Model): me = models.ForeignKey(User) friend = models.ForeignKey(User) remark =
I have two models like this: class OptionsAndFeatures(models.Model): options = models.TextField(blank=True, null=True) entertainment =
I have two models in Django like follows(in pseudo code) class Medicine(db.Model): field_1 =
I have two models Doctors and Questions like the follwong: Doctor Model class Doctor
I have two models: class User end class Message belongs_to :sender, :class_name=> 'User' belongs_to
I have a blog-like application with stories and categories: class Category(models.Model): ... class Story(models.Model):
I have two models, Tweets and TweetsLocations, associated to each other with: class Tweets
I have two models Horse and Race. class Horse < ActiveRecord::Base has_many :races end
I have two models: User and Base. User model: http://pastebin.com/WdLzBkHJ Base model: http://pastebin.com/tQrEUaSu At
I have two models in Django linked together by ManyToMany relation like this: class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.