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*
Related
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...
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 new to Django and I am wondering about how to work with Django when it comes to an existing database
For instance, let say we have the following tables
Table Student
ID primary key
First_Name Text
Last_Name Text
Table Classes
CID primary key
SKF ForeignKey
Class Text
Ok, this database is already created and has data inside and the foreign and primary keys are already set up. Now, when creating our model, how do we tell Django the foreignkey is the SKF field? If you need any more information please let me know. Thank you for your time.
Django has a built-in function (inspectdb) for auto-generating models from an existing database.
Of course it also takes care of generating the corresponding ForeignKeys.
I am using Zinnia Blog module with my Django app. I have a Django app and have a custom User model. I have added the following setting:
AUTH_USER_MODEL = 'account.Member'
However, when I try to create a new entry in the blog I get the Foreign Key constraint error.
Exception Type: IntegrityError
Exception Value:
(1452, 'Cannot add or update a child row: a foreign key constraint fails (`example`.`zinnia_entry_authors`, CONSTRAINT `author_id_refs_id_410656e2874b02ff` FOREIGN KEY (`author_id`) REFERENCES `auth_user` (`id`))')
It references the default auth model (which doesn't exist in the database) instead of the specified auth model. How do make Zinnia use the correct table.
Thanks.