如何使用Django与遗留只读数据库表与复合主键? [英] How to use Django with legacy readonly database tables with composite primary keys?

查看:677
本文介绍了如何使用Django与遗留只读数据库表与复合主键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要将Django用于具有旧数据库的客户端项目。如果在所有可能,我想能够使用Django管理界面。但是,数据库具有多列主键的表,我看到Django不喜欢 - 管理界面抛出一个MultipleObjectsReturned异常。



这里有什么选项?我可以添加新表,但我不能更改现有表,因为其他项目已经添加数据到数据库。我已经看到了提及代理键的其他问题,但它似乎需要更改表。



编辑:有问题的数据库是一个MySQL数据库。 >

解决方案

您正在谈论一个传统的READONLY数据库,也许你可以创建一个没有多列PK的外部模式(视图)。例如,您可以连接字段键。这里和示例:



例如:



表:


$ b b

  create table A(
a1 int not null,
a2 int not null,
t1 varchar(100),
primary key(a1,a2)


创建表B(
b1 int not null,
b2 int not null,
a1 int not null,
a2 int not null,
t1 varchar(100),
主键(b1,b2),
约束b_2_a外键(a1,a2)
引用A (a1,a2)

要由django读取的外部模式:

 将视图vA创建为
select
a1 * 1000000 + a2 as a,A. *
A

将视图vB创建为
select
b1 * 1000000 + b2 as b,
a1 * 1000000 + a2 as a,B. *
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 = ...
类Meta(CommonInfo.Meta):
db_table ='vB'
pre>

您可以细化技巧,使varchar键能够使用索引



更多信息:



Do Django模型支持多列主键?



机票373



替代方法


I want to use Django for a client project that has a legacy database. If at all possible I would like to be able to use the Django admin interface. However, the database has tables with multicolumn primary keys, which I see Django does not like - the admin interface throws a MultipleObjectsReturned exception.

What are the options here? I can add new tables, but I can't change existing tables since other projects are already adding data to the database. I've seen other questions mentioning surrogate keys, but it seems like that would require changing the tables.

EDIT: The database in question is a MySQL database.

解决方案

You are talking about a legacy READONLY database then, perhaps you can create an external schema (views) with no multi-column PKs. For example you can concatenate field keys. Here and example:

For example:

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)
)

External schema to be read by 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 models:

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'    

You can refine technique to make varchar keys to be able to work with indexes. I don't write more samples because I don't know what is your database brand.

More information:

Do Django models support multiple-column primary keys?

ticket 373

Alternative methods

这篇关于如何使用Django与遗留只读数据库表与复合主键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆