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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:14:32+00:00 2026-05-23T12:14:32+00:00

I have user profile model with M2M field class Account(models.Model): … friends = models.ManyToManyField(‘self’,

  • 0

I have user profile model with M2M field

class Account(models.Model):
    ...
    friends = models.ManyToManyField('self', symmetrical=True, blank=True)
    ...

Now I need to know HOW and WHEN add each other as a FRIEND
And I created a model for that

class Account(models.Model):
    ...
    friends = models.ManyToManyField('self', symmetrical=False, blank=True, through="Relationship")
    ...


class Relationship(models.Model):    
    """ Friends """        
    from_account = models.ForeignKey(Account, related_name="relationship_set_from_account")            
    to_account = models.ForeignKey(Account, related_name="relationship_set_to_account")
    # ... some special fields for friends relationship

    class Meta:                    
        db_table = "accounts_account_friends"            
        unique_together = ('from_account','to_account')

Should I create any migration for this changes or not ?
If you have any suggestions you are feel free write their here.

Thanks

PS: accounts_account table already contain records

  • 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-23T12:14:33+00:00Added an answer on May 23, 2026 at 12:14 pm

    First off, I’d avoid using the db_table alias if you can. This makes it harder to understand the table structure, as it is no longer in sync with the models.

    Secondly, the South API offers functions like db.rename_table(), which can be used by manually editing the migration file. You can rename the accounts_account_friends table to accounts_relation (as Django would name it by default), and add the additional columns.

    This combined gives you the following migration:

    def forwards(self, orm):
        # the Account.friends field is a many-to-many field which got a through= option now.
        # Instead of dropping+creating the table (or aliasing in Django),
        # rename it, and add the required columns.
    
        # Rename table
        db.delete_unique('accounts_account_friends', ['from_account', 'to_account'])
        db.rename_table('accounts_account_friends', 'accounts_relationship')
    
        # Add extra fields
        db.add_column('accounts_relationship', 'some_field',  ...)
    
        # Restore unique constraint
        db.create_unique('accounts_relationship', ['from_account', 'to_account'])
    
    
    def backwards(self, orm):
    
        # Delete columns
        db.delete_column('accounts_relationship', 'some_field')
        db.delete_unique('accounts_relationship', ['from_account', 'to_account'])
    
        # Rename table
        db.rename_table('accounts_relationship', 'accounts_account_friends')
        db.create_unique('accounts_account_friends', ['from_account', 'to_account'])
    
    
    models = {
        # Copy this from the final-migration.py file, see below
    }
    

    The unique relation is removed, and recreated so the constraint has the proper name.

    The add column statements are easily generated with the following trick:

    • Add the Relationship model in models.py with foreign key fields only, and no changes to the M2M field yet.
    • Migrate to it
    • Add the fields to the Relationship model.
    • Do a ./manage.py schemamigration app --auto --stdout | tee final-migration.py | grep column
    • Revert the first migration.

    Then you have everything you need to construct the migration file.

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

Sidebar

Related Questions

I have the following models: class Application(models.Model): users = models.ManyToManyField(User, through='Permission') folder = models.ForeignKey(Folder)
I have following model: class UserProfile(models.Model): User profile model, cintains a Foreign Key, which
I have a simple question. This is my profile: class Profile(models.Model): user = models.ForeignKey(User,
I have two models Profile and Avatar. models.py class Profile(models.Model): user = models.ForeignKey(User) profile_name
I have a model as follows class UserPrivacy(models.Model): user = models.ForeignKey(User) profile = models.SmallIntegerField(default=1,
Given these two models: class Profile(models.Model): user = models.ForeignKey(User, unique=True, verbose_name=_('user')) about = models.TextField(_('about'),
I have created the following M2M table with an intermediary model -- class Position(models.Model):
15 class Profile(models.Model): 16 17 User profile model 18 19 user = models.ForeignKey(User, unique=True)
I have 2 Models: # user.rb class User < ActiveRecord::Base has_one :profile, :dependent =>
In my CakePHP 1.2.5 app, I have a Profile model that belongsTo a User

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.