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 5841397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T11:53:07+00:00 2026-05-22T11:53:07+00:00

Seems like this should be easy or at least documented somewhere, I just cant

  • 0

Seems like this should be “easy” or at least documented somewhere, I just cant find it.

Lets say I have a model:

class A(models.Model):
    users = models.ManyToMany('auth.User', blank=True)

Now I want to migrate to have a through table to add fields to the ManyToMany relation…

class AUsers(models.Model):
    user = models.ForeignKey('auth.User')
    a = models.ForeignKey('A')
    new_field = models.BooleanField()

class A(models.Model):
    users = models.ManyToMany('auth.User', blank=True, through='AUsers')

Then I do:

% ./manage.py schemamigration app --auto

Not totally surprising, it tells me it is going to drop the original auto-created through table and create a new one for AUsers. What’s the best practice at this point? Is there a decent way to migrate to the new through table? Do I use db_table in Meta? Do I just not use the through=... right away… then do a schemamigration --auto, then a datamigration to copy the current table (somehow, not sure…) and then add the through relation and let it kill the table?

What’s the trick here? Is this really that hard?

  • 1 1 Answer
  • 0 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-22T11:53:07+00:00Added an answer on May 22, 2026 at 11:53 am

    You should be able to do this pretty easily.

    First of all, make sure that the manual through table that you are creating has the same table name in the database as the one Django originally created automatically.

    So, first, let’s consider a manual through model before your change:

    class AUsers(models.Model):
        user = models.ForeignKey('auth.User')
        a = models.ForeignKey('A')
    
        class Meta:
            db_table = 'appname_a_user'
    

    That should be functionally (almost) identical to the ManyToManyField you used to have. Actually, you could make an empty migration and apply it, and then use –auto for your changes (but don’t).

    Now, add your field like you did in your sample code above, and then run ./manage.py schemamigration appname manual_through_table --empty. That will give you an empty migration named ####_manual_through_table.py.

    In the migration itself, there will be a forwards and backwards method. Each one needs to be one line each:

    def forwards(self, orm):
        db.add_column('appname_a_user', 'new_field', self.gf('django.db.models.fields.BooleanField')(default=False))
    
    def backwards(self, orm):
        db.delete_column('appname_a_user', 'new_field')
    

    That should get you what you are after.

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

Sidebar

Related Questions

This seems like it should be easy, but I can't quite find an explanation
This seems like it should be really easy but I couln't find an answer
This seems like this should be easy but I have not been able to
This seems like it should be an easy thing to do, but for the
This seems like it should be very easy...anyway, it is in MS SQL Server
This seems like it should be something very easy to do, but every time
Seems like this should be easy, but I'm not finding a simple configuration setting.
This seems like it should be easy, but I can't get the right syntax.
Seems like this should be an easy task but I can't figure out how
Seems like this should be obvious, but how do I send arrow key presses

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.