I have a model which is named Client. It's defined in models in part as the following:
class Client(models.Model):
username = models.OneToOneField(settings.AUTH_USER_MODEL,
on_delete=models.CASCADE)
I've run makemigrations and migrations, but when I create a new client object and try to save it from forms I get and Integrity Error: NOT NULL constraint failed: intake_client.username_id.
username_id is the field name Django automatically generates with the migration, but saving these forms results in this field containing a null value and is not being generated automatically as a dependency by Django. If I set primary key to true in the username, it works ok, but then I have problems deleting the objects because Django says the model does not have an id field (since it is now changed to username_id). Why is Django not generating and putting the dependency values in the username_id field automatically when the instance is saved?
Related
I'm using an existing mysql database in a new django project, so I generated the models using inspectdb tool.
But now I need to add a new field to a table, I'm doing it by adding the new field to the model and running migrations, but it doesn't work, It doesn't add the field to the table.
Maybe it is because I have managed=False in the meta config of the model, but if I remove it, the migrations won't work, giving me the error "Table5 already exists"
Here is the Model where I'm trying to add the "fields" field
class Table5(models.Model):
names = models.CharField(max_length=150)
fields=models.JSONField(null=True)
class Meta:
managed = False
db_table = 'table5'
How can I achieve this?
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.
How to set username as ForeignKey in Django module.
is bellow method is correct?
user = models.ForeignKey(User, db_column="user")
i cannot use ID as ForeignKey because my old db have username as ForeignKey.(I need to migrate the the old Data)
Use below code:
user = models.ForeignKey(User, to_field="username")
The field on the related object that the relation is to. By default,
Django uses the primary key of the related object. If you reference a
different field, that field must have unique=True.
I currently have a Django powered in-production web app that contains multiple models, sitting on top of a Postgresql database (Google Cloud SQL)
During initial set-up, one of the models was set up as follows:
class ExampleModel(models.Model):
id = models.CharField(max_length=60, unique=True, primary_key=True)
new_id = models.CharField(max_length=60, unique=True, null=True, db_index=True)
name = models.CharField(max_length=300, db_index=True)
tags = models.ManyToManyField(Tag, blank=True)
The id field contains a unique ID like: AB123456789.
I have since realised this is a mistake and would like to revert the primary key field to a standard auto-incrementing autofield, and instead use the 'new_id' field to store the unique ID.
Please can someone provide guidance on how I can make this change and perform the necessary database migrations? There are a number of foreign key fields in other models that currently use the id field in the above model which will need changing. As you can see in the above, there is also a many to many field between this model and a tag model.
I tried removing the id field from my models.py file and migrating - it initially gave an error linked to null fields and default values so I set a dummy default value in the Terminal window and removed this in the migration file.
The database removed the id field successfully and generated a new Autonumber primary key field however none of the many to many or foreign key relationships were kept post migration. I have since rolled back to a prior version of the database.
Generally this will be your approach. Steps 1-4 can be merged into a single deployment. Steps 5-7 into another. Then 8-9 would be the final.
Create new auto field.
Create new FK nullable relationships on models
Update all code that creates related models to populate both FK's
Populate all the null FK fields via a script
Make the new FK fields not-nullable
Make old FK's nullable.
Remove old FK usages from code base
Migration to remove old ID field and FKs.
(optional) Rename auto field to be ID and potentially use Django's built-in field.
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.