Django: Foreign Key to a partitioned table - python

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.

Related

Reverse foreign key relation in Django ORM

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)

Control the name for a constraint in django

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.

Django admin edit integer field

I have a model with two integer fields which are supposed to be foreign keys. In my admin view, those two fields are editable but I would like to display a search icon next to the field, so I can retrieve the id of another model and append to the field. Does django have this feature?
class Linkage(models.Model):
user_id = models.IntegerField(default=1)
parent_id = models.IntegerField(default=0)
If your model has 2 fields that should actually be foreign keys, then you need to use ForeignKey instead of IntegerField as so:
from django.db import Models
from app.models import yourModel
class Linkage(models.Model):
user_id = models.ForeignKey(yourModel)
parent_id = models.ForeignKey(yourModel)
This Django feature will take care of linking your Linkage to your other model of choice automatically. In the admin, you will no longer need to set an integer with the id of the foreign key, you will be able to simply select an instance of the linked model.
You can read more about Django fields references here.
EDIT: In the way you designed yoour models, there might for example not be a parent_id, so you can add options such as null=True to the parent_id field as so:
parent_id = models.ForeignKey(yourModel, null=True)
I strongly recommend that you read the Django documentation, it is really good for beginners as it is very detailed and clear. You can access the documentation here.
They also have a set of tutorials to get you through the steps and introduce you to the different basics you need to know to get started with Django here.

How do I make a primary key without auto-increment?

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.

Uniqify Django models with a unique attribute instead of using PK

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.

Categories

Resources