rest_framework.authtoken app migration error - django

I am not able to install rest_framework.authtoken app in custom User model.
The User model table is assigned name users with db_table=u'user'. While running migration of rest_framework.authtoken gives error
Running migrations for authtoken:
- Migrating forwards to 0001_initial.
authtoken:0001_initial
FATAL ERROR - The following SQL query failed: ALTER TABLE "authtoken_token" ADD CONSTRAINT "user_id_refs_id_14b35167" FOREIGN KEY ("user_id") REFERENCES "users_user" ("id") DEFERRABLE INITIALLY DEFERRED;
The error was: relation "users_user" does not exist
Error in migration: authtoken:0001_initial
DatabaseError: relation "users_user" does not exist
setting have been configured in setting.py with AUTH_USER_Model="users.User". The table in database exist with name user.But, still i am receiving error. Can somebody point out my error. Thank you.

Related

Change properties of a foreign key field in default django table

I am trying to use an existing database with django ORM.
I have created the models for the tables that I want to use.
python manage.py makemigrations ran without any problem.
python manage.py migrate returns the error
File "/home/s30python-02/anaconda3/envs/test/lib/python3.10/site-packages/pymysql/err.py", line 143, in raise_mysql_exception
raise errorclass(errno, errval)
django.db.utils.OperationalError: (1005, 'Can't create table site_db.django_admin_log (errno: 150 "Foreign key constraint is incorrectly formed")')
I had read that the FOREIGN KEY references in MySQL is that both columns of the constraint must the same column definition.
So I checked and the DataType of primary key of user table is int(11) unsigned and the user_id in django_admin_log table is bigint(20).
And I think the problem is due to that.
Is there a way to override LogEntry which is responsible for the django_admin_log table and change the user_id field properties or is there anything else that can be done.
Need your insight on this one.
Thanks for your time...

Django SQLite3 to PostgreSQL - duplicate key error on OnetoOne User Model

I'm having some trouble with migrating from SQLite3 to Postgres on Django. Essentially I'm following this thread (Django: What are the best practices to migrate a project from sqlite to PostgreSQL). There's been a similar error that I've used the TRUNCATE command on and got past, but now I'm hitting a duplicate key error for my own model.
The model client is a OnetoOne relationship with Django's built in user. It just asks for a few additional details like business type and join date. And the migration is coming up with an error. The client, primary key 1 is my superuser with code:
user = models.OneToOneField(User, on_delete=models.SET_NULL, null=True, blank=True)
And I'm getting this error:
django.db.utils.IntegrityError: Problem installing fixture 'C:\Users\*******\dump.json': Could not load clauses.Client(pk=1): duplicate key value violates unique constraint "clauses_client_clientusername_id_******_uniq"
Could someone point me the right way? Thanks!

rest_framework authtoken migration error

Unable to complete migration for rest_framework.authtoken
Running migrations for authtoken:
- Migrating forwards to 0001_initial.
authtoken:0001_initial
FATAL ERROR - The following SQL query failed: ALTER TABLE "authtoken_token" ADD CONSTRAINT "user_id_refs_id_14b35167" FOREIGN KEY ("user_id") REFERENCES "users_user" ("id") DEFERRABLE INITIALLY DEFERRED;
Error in migration: authtoken:0001_initial
DatabaseError: relation "users_user" does not exist
Using version djangorestframework-2.3.8
Because of the custom user table name the migration isn't happening. Go to the initial migration and specify your user table using db_table in the migration where the code is trying to access your custom table. That should work.
are you using a custom user model?
if this was the case the
migration code for the app implementing the user model should look like:
class Migration(SchemaMigration):
needed_by = (
('oauthtoken', '0001_initial'),
)

Syncdb error when creating CustomUser

The issue is that I create Custom User:
class CustomUser(AbstractUser):
mobile = models.CharField(max_length=16)
address = models.CharField(max_length=100)
And in settings.py write this:AUTH_USER_MODEL = 'login.CustomUser'
And when I run manage.py syncdb for the first time(when db doesn't contains any tables), it throws an error: django.db.utils.ProgrammingError: relation "auth_group" does not exist
But when I run manage.py syncdb when db with tables already exist, then it's okay, just create additional tables. What's wrong to run it when db doesn't contain tables?
Try this
Create a migration directory in the directory of the app containing your custom user model.
Create an empty __init__.py file inside the migration directory.
Execute ./manage.py makemigrations.
Execute ./manage.py migrate.
I think when you use a custom user model, you need to create migrations. Besides, syncdb is already deprecated and will be removed in Django 1.9 so it's better to start using the ./manage.py migrate command.
As the error says, Your CustomUser table has relation with auth_group table, so unless that table is created or exists, you cannot create this new table in db.
When you say 'db with tables already exist, then it's okay', did You create the tables manually or included admin app in your INSTALLED_APPS?
Because if u include admin app, it will automatically create the auth_group table, so you will not see that issue

Upgrading existing django_comments to Django 1.5

I have a Django 1.4 site that uses the Django comments app. I'm upgrading my dev version to Django 1.5 and extending the User model - I have a Person model which extends AbstractBaseUser, and AUTH_PROFILE_MODULE = 'membership.Person' in my settings.
At the moment the django_comments postgresql database table has a column user_id which references auth_user(id):
"django_comments_user_id_fkey" FOREIGN KEY (user_id) REFERENCES auth_user(id) DEFERRABLE INITIALLY DEFERRED
There's nothing in that column because, so far, there are no user accounts in the system, other than my own admin user. I think I need to update the table to refer to my new Person model (which is stored in the membership_person table). In the future users will be able to create accounts and post logged-in comments.
What ALTER TABLE command should I use to change django_comments to refer to my new Person model? I'll have to do similar to the django_comment_flags table. Is there anything else I should watch out for?
I ended up doing this for the comments table:
ALTER TABLE django_comments DROP CONSTRAINT django_comments_user_id_fkey;
ALTER TABLE django_comments ADD CONSTRAINT django_comments_user_id_fkey FOREIGN KEY (user_id) REFERENCES membership_person(id) DEFERRABLE INITIALLY DEFERRED;
And similar for the comment flags table:
ALTER TABLE django_comment_flags DROP CONSTRAINT django_comment_flags_user_id_fkey;
ALTER TABLE django_comment_flags ADD CONSTRAINT django_comment_flags_user_id_fkey FOREIGN KEY (user_id) REFERENCES membership_person(id) DEFERRABLE INITIALLY DEFERRED;
Hopefully that does the job and doesn't cause any problems. *crosses fingers*