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...
Related
I just switched out the django User model for a CustomUser model. I've got a field called contributor_id in a Project model which is a m2m field connected to the User model. I've told the m2m field to point to the CustomUser model and have run makemigrations and migrate but the user_id field has not changed to customuser_id. This is causing the following error:
Unknown column 'projects_project_contributor_id.customuser_id' in 'field list'
I found these bug reports: bug1 and bug2 but they both appear to have been fixed in 2.0. Obviously I can just run an alter table query on my database, but I don't know if:
a) There's away to get the django orm to do the change, and
b) if altering the tables directly will get my migrations out of wack.
I needed to change my foreign key on my User model from a UUIDField to an IntegerField. I did this in two steps:
Rename the current primary key from id to old_id and run a migration. This generated a RenameField operation.
Deleted the old_id field entirely from User so that the automatic id field would take over. This generated a RemoveField operation on old_id and an AddField operation for a models.AutoField named id. Perfect. The database shows that User now has an int id field, auto-incrementing.
Then I went to run the app and quickly ran into a problem: the database join tables automatically generated by the models.ManyToManyFields on User (e.g. user_languages weren't updated - they still have both a language_id AND a user_id that are UUID data types. There is no foreign key constraint in the database, but there are indexes on that field.
How can I force Django to regenerate those join tables with the new data type for the User.id column?
Notes: manage.py makemigrations doesn't pick up any pending changes. Also, I'm OK with losing the data in the db.
Remove/comment out fields from the model:
class User(models.Model):
# languages = models.ManyToManyField('Language')
# other fields
Then generate a migration. Then uncomment the line(s). Then run another migration.
Warning: This will cause those tables to be dropped and re-created, so all those relationships will be lost.
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'),
)
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.
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*