I have used my own user model till now and suddenly i realized that django's user model offers me exactly the things that i would have to create on my own... why not to take django's user model..
now i changed the user model and made it inherit from django user model by adding:
class User(User)..
and tried to migrate the db with these steps:
1. python manage.py schemamigration home --auto
-- it asked me to give some default values for these two columns I mean down here
2. python manage.py migrate home
3. error
3.error says that user_ptr_id exists double and thats why couldnot create the unique index user_pkey.
what can i do now so that i can migrate?
Related
I have walking on a strange way, but it is result of circumstances.
I had to been generate single models.py from exist postgres DB by inspect_db.
Next I fix some field (was a few problem with keys), and create 2 apps inside this project. So now I have 3 models.py (them are split models.py, whitch was generate by inspect_db). managed = True was added. Classes have same links and datatypes like in the database
Next I wish to integrate this models to exist database. I make migration, I migrate and in the end DB have only system django tables (auth_, django_migrations etc.) None from my classes (although migrate files were create). So I tryied to delete migrations catalogs and repeat makemigrations and migrate, but terminal threw it:
Operations to perform:
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
No migrations to apply.
(forest-venv) user#user-HP-Z800-Workstation ~/MyProjects/forest-venv/lesoved $ python manage.py runserver 8001
Performing system checks...
If I try make migrates again - no changes. I tryied to delete info about my apps in django_migrations table - no result.
So my questions:
- It is possible to integrate new models into exist database (if names, keyes and formats is ok)?
- Integration is possible by the way where data from exist database is saved after migrations?
- How to return possobillity of make migrations? now it isn't work.
The trick when using an existing database is to make sure you start from a well-defined state: First get Django to the exact same state as your db, and only after that start making changes to your models. So these are the steps:
Check that your database doesn't have a django-migrations table, delete it using SQL if needed. (Note: I'm assuming this db isn't generated by Django to start with and you're creating a fresh django application)
Create your models with inspectdb. If needed, rename the models to have proper CamelCase. If you rename models or fields that would change the table or column name in the db, make sure to set db_table (Meta options of your model) and db_column (as field attribute).
Run manage.py makemigrations. Check the migration files for your models, just to be sure the names match with your db.
For your own apps, run manage.py migrate <app> --fake. This will add the migrations to django-migrations table in your db as if they ran, without actually running them.
Then run manage.py migrate to create the django provided tables (auth, contenttype, session etc...)
Now you are at the state where you can start changing things. If you change the model and run makemigrations it should create a migration just for your changes.
I added this module to my application: https://github.com/tomwalker/django_quiz.
The module has a submodule named quiz, which has a model class called Question.
I want to change that class so that it corresponds to a user so I added this to the submodules's models.py:
user = models.ForeignKey(
User,
verbose_name=_("UserId")
)
I am now trying to generate the corresponding database migrations.
However, when I run python manage.py makemigrations nothing happens.
Why is that the case?
You claim some migrations were made. Well probably Foreign Key was created successfully. If you added some parameters to Foreign Key, this might not be needed to migrate database, but Django will take that change into account.
It is probably your case. Try to run python manage.py migrate quiz and see if it works as you wanted in the first place. If not, let us know what setting is missing.
I've just started working with Django, and when setting up my models and db I picked a name which I would like to change now. Is it OK to just edit models.py (rename the class), then run makemigrations and migrate on it? I have a table set up in the db (SQLite), but no entries yet.
I'm new to database migration too. Does this cover what I want to do?
Thanks.
After renaming of model in the models.py, run makemigrations. The question should appear "Did you rename the xxx model to yyy? [y/N]" Press y and it will be enough in your case.
UPD:
In general, you should run makemigrations and migrate every time when you change code in models.py. You should manually edit migrations files (created by makemigrations) only in few cases, when the Django isn't smart enough to understand what you want to do.
I created a custom user model in Django and it worked up fine. However, I decided to create a custom model to suit my needs after the project was up and running.
As a result, I will need to migrate the schema (Currently, when I register a user, the code is still referencing to the the auth_user database tables where as the new custom user table is user.)
I have set the AUTH_USER_MODEL in settings.py to userapp.User, where userapp is my custom user app and User is the Model that inherits from the AbstractUser model.
I am fairly new to Django and cannot understand how to achieve this. One obvious way to clean install the database, which is not something that I'm looking to do as it will remove all my data.
How do I migrate then? I've heard South is used for that but I don't know how to use it. Besides I think South isn't required in the recent versions of Django.
My version of Django is 1.8.2.
I did this recently - we used a data migration to move between the two models. Rough steps:
Create the new user model (without telling Django about it), make/apply migrations to create the database table(s)
Write a the data-migration to copy the data over to the new user model. Then run this migration and update Django to use the new model
Now you should be switched over, and can delete/archive the old auth_user table as needed
The problem is pretty self-explanatory in the title. Do I need to do that or I just need to edit the existing migration file?
Yes, Django won't recognize the field if you change the name. I will say that the "field does not exist", so YES, you have to run Django's South migrate / schemamigration as you asked.
Datatype YES as well. Django may be okay at first if you only change the field type depending, but may run into problems later depending on what you have in that field.
You need to do a schemamigration every time you change your models.
On every call of python manage.py migrate command south record number of the latest migration applied into database migrationhistory table. So if you just change existing migration it won't be applied because south would think it's already applied.
You can make a backward migration, fix next migration, even delete it and make a new one and only then migrate forward.