您正在谈论旧版 READONLY 数据库,那么也许您可以创建一个没有多列 PK 的外部架构(视图)。例如,您可以连接字段键。这里和例子:
例如:
Tables:
create table A (
a1 int not null,
a2 int not null,
t1 varchar(100),
primary key (a1, a2)
)
create table B (
b1 int not null,
b2 int not null,
a1 int not null,
a2 int not null,
t1 varchar(100),
primary key (b1, b2),
constraint b_2_a foreign key (a1,a2)
references A (a1, a2)
)
django 读取的外部模式:
Create view vA as
select
a1* 1000000 + a2 as a, A.*
from A
Create view vB as
select
b1* 1000000 + b2 as b,
a1* 1000000 + a2 as a, B.*
from B
Django 模型:
class A(models.Model):
a = models.IntegerField( primary_key=True )
a1 = ...
class Meta(CommonInfo.Meta):
db_table = 'vA'
class B(models.Model):
b = models.IntegerField( primary_key=True )
b1 = ...
a = models.ForeignKey( A )
a1 = ...
class Meta(CommonInfo.Meta):
db_table = 'vB'
您可以改进技术以使 varchar 键能够使用索引。我不写更多示例,因为我不知道你的数据库品牌是什么。
更多信息:
Django 模型支持多列主键吗? https://docs.djangoproject.com/en/dev/faq/models/#do-django-models-support-multiple-column-primary-keys
票 373 https://code.djangoproject.com/ticket/373
替代方法 https://code.djangoproject.com/wiki/MultipleColumnPrimaryKeys