I have to update a record which have foreign key constraint.
i have to assign 0 to the column which is defined as foreign key
while updating django don't let me to update record.
ForeignKey is a many-to-one relationship. Requires a positional argument: the class to which the model is related.
It must be Relation (class) or Null (if null allowed). You cannot set 0 (integer) to ForeignKey columnn.
Related
How to return all objects instances of a particular model that are being addresed as Foreign key by ANY object instance of a different model ? Lets say there is a model Item and a model ItemRequested that has foreign key relation to Item. How to print all Items that are being mentioned as foreign key in the ItemRequested table/model ? basically this is the SQL query that i want to execute in Django:
select * from backend_item where id in (select id from backend_itemrequested);
Obviously i want to avoid executing raw SQL commands from inside Django ORM
I'm not sure exactly what you're asking here. But perhaps this is what you want:
Item.objects.exclude(itemrequested=None)
I have the problem that I need to replace a FOREIGN KEY constraint, generated by django, by a ON DELETE CASCADE constraint. What I want to do is to extend the django generated migration to implement the following with RunSQL:
Drop the constraint generated by Django with DROP FOREIGN KEY
Create a new constraint with ON DELETE CASCADE.
To perform the first operation, I need the name of the FOREIGN KEY constraint, but this is automatically generated by django.
How can I force the name of a FOREIGN KEY constraint? (not the name of the FOREIGN KEY column, which is a different thing)
There is a simple solution: do not allow django to create the foreign key constraint in the first place. This can be achived with db_constraint=False:
tenant = models.ForeignKey(Tenant, on_delete=models.CASCADE, null=True, db_constraint=False)
After that, you can put any constraint you like, using migrations.RunSQL.
I have used architect to partition an existing table.
#architect.install('partition', type='range', subtype='integer', constraint='100', column='id')
class Project(models.Model):
name = models.CharField(max_length=150)
The project model was used as a foreign key to another model.
class ProjectChangeLog(models.Model):
project = models.ForeignKey(Project, on_delete=models.CASCADE)
After partitioning, I could not add any ProjectChangeLog objects. It says:
IntegrityError: insert or update on table "logging_projectchangelog" violates foreign key constraint
DETAIL: Key (project_id)=(231) is not present in table "project_project"
One of the Limitations of partitions is that you can't have foreign keys pointing to them. One workaround for this is to use custom constraints to mimic ForeignKey behavior. This method can help you.
I've been trying to create a model that has a primary key, but I don't want that primary key to auto increment.
I know I can specify the value each time, but I want the field to be required that I specify it (hopefully enforced by the database and django), and fail fast if I forget.
It seemed logical that I would be able to say auto_increment=False on my field, but that isn't supported by the field :(
Just create id field with primary_key=True explicitly in your model:
class SomeModel(models.Model):
id = models.IntegerField(primary_key=True)
That way it won't be auto-incremented, but it will still be an primary key.
In the Django docs it was stated that besides the Primary Key, if another attribute has the setting "unique" set to True, an IntegrityError would be thrown if a model with the same attribute was being added to the database (much like how the PK is handled).
However, even after setting the unique = True to one of my fields, no IntegrityErrors are thrown, and in the manage.py shell, i'm blatantly saving models with identical fields where the unique = True, and it's letting me.
The PK of my models is unset, aka the AutoIncrementing Integer (i think this may be part of the problem).
Here is my unique=True field.
url = models.URLField("The URL", unique=True)
Nothing else is notable about this model, no Foreign relationships, nothing. Just A unique field which must be enforced (but is not), and an auto incrementing PK.
For the sake of some search engines, the PK must remain an auto incrementing integer.
Here is the SQLall for the model:
BEGIN:
CREATE TABLE `Model_model` (
`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
`url` varchar(200) NOT NULL UNIQUE,
....
Thank you.
Try dropping and recreating your database. The unique constraint will not be updated in database, if you add it after creating the database tables. If you don't want to lose your data, you need to add the property manually in the database.