0

I have created a UserProfile field in order to add a favorites functionality to my site. Using Django's recommendation, I created a UserProfile model as follows at the bottom

Unfortunately, I already had the rest of my database created, and so I need to either use a migration utility or manually edit my database. However, I do not have sufficient permissions to utilize a migration utility, so I have to edit the database directly, and am struggling to do so.

This answer is similar to what I want to accomplish, but I can't quite get the syntax to work in my case. MySQL - One To One Relation?

class UserProfile(models.Model):
    user = models.ForeignKey(User, unique=True)
    favorites = models.ManyToManyField(Media, related_name='favorited_by')
Community
  • 1
  • 1
Shaun Singh
  • 519
  • 4
  • 11

2 Answers2

0

In my experience, the best migration utility is South. Once you've installed and added it to your settings, you'll need to create initial migrations for your existing modules using

./manage.py schemamigration --initial my_module,

which will include the one containing your UserProfile model, then from there you can migrate using

manage.py migrate my_module.

The real power in using a utility like this is portability and reversibility. You can migrate forward and backward as needed, and you'll be able to bring your schema to virtually any SQL database without all the fuss of rebuilding using SQL directly.

Steve
  • 791
  • 6
  • 6
0

I would certainly agree with Steves recommendation to use South. However if you for some reason wouldn't want to, you can issue the following command:

python manage.py sql <appname>

This will output the SQL statements which django will use to create your tables. This can then be used to manually modify the database.

Erik Näslund
  • 1,196
  • 11
  • 9
  • I was manipulating the database through direct mysql command line operations, how can you edit using the managd.py tool? – Shaun Singh Jun 18 '13 at 04:22
  • Djangos only support for 'editing' using manage.py is through migrations. The sql command is only useful for getting the correct SQL to create / modify your tables. You would still have to execute it through a tool such as MySQL workbench. – Erik Näslund Jun 18 '13 at 05:07