Skip to content

Instantly share code, notes, and snippets.

@bengolder
Last active January 19, 2017 01:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bengolder/bdd3669c01d710e8d6e588b0c2493c78 to your computer and use it in GitHub Desktop.
Save bengolder/bdd3669c01d710e8d6e588b0c2493c78 to your computer and use it in GitHub Desktop.
# in intake.models
class FormSubmission(models.Model):
	organizations = models.ManyToManyField('user_accounts.Organization')

# in user_accounts.models
class Organization(models.Model):
	# some attributes

We now added an explicit through model

# in intake.models
class Application(models.Model):
	organization = models.ForeignKey('user_accounts.Organization')
	form_submission = models.ForeignKey(
		'intake.Organization', db_column='formsubmission_id')

	class Meta:
		db_table = 'intake_formsubmission_organizations'
		auto_created = True


class FormSubmission(models.Model):
	organizations = models.ManyToManyField(
		'user_accounts.Organization',
		through='intake.Application')

Everything up to this point is fine. Tests work. We can create models and access relational queries.

We did not make a new migration because the table already existed and no changes to the database were needed.

Then we added a ForeignKey that points to the through model

# in intake.models
class StatusUpdate(models.Model):
	application = models.ForeignKey('intake.Application')

When we try to make migrations for this new StatusUpdate model, Django returns an error

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment