简体   繁体   中英

Django: Foreign Key to a partitioned table

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM