Zaph
Zaph

Reputation: 13

Changing foreign key to another model

I have a model:

class myModels(model.Model):
    handler =  models.ForeignKey(User, blank=True, null=True)

The foreign key hasn't been used so it's always null. Now I want to use it as a foreign key to another model, like this:

handler =  models.ForeignKey(Something, blank=True, null=True)

I'm using MySQL.The handler has constraints to User model.

Suggestions on how to solve this would be welcome. My first thought would be to drop the column and add it again with the new constraint, but I'm not sure whether dropping a column gets rid of the constraint.

Upvotes: 1

Views: 122

Answers (1)

jpic
jpic

Reputation: 33410

You can do ./manage.py reset yourapp which will drop the tables of your app, then you'd syncdb again. That's what I do during development because it's quick and easy.

There are many other ways: you can install django-extensions, run command ./manage.py sqldiff yourapp, and then run the generated SQL, which will modify the structure according to your models.py.

If you want to know more about schema migrations, here is an article which gives an overview of the current options.

Upvotes: 3

Related Questions