how to description FK which contains more than 1 field

10 views
Skip to first unread message

Holland_zwz

unread,
Aug 10, 2015, 5:39:13 AM8/10/15
to Django users
Hi all,

   I have 2 tables, one is A another is B, and B have one FK which contains more than 1 field references to A. The table struct is here:

create table A
(
  oid int not null,
  types varchar(20) not null,
  name varchar(30) null,
  color varchar(20) null,
  constraint PK_A primary key (oid,types)
)

create table B
(
 oid int not null,
 types varchar(20) not null,
......
)
alter table alter B  add constraint B_FK foreign key (oid,types) references A(oid,types) 



The how can i description the ModelB'f FK in Model?

class ModelA(models.Model):
    oid = models.IntegerField(max_length=10, primary_key=True)
    name = models.CharField(max_length=30,  primary_key=True)
    ....


class ModelB(models.Model):
    oid = models.ForeignKey(ModelA, to_field='oid')
    types = models.ForeignKey(ModelA, to_field='types')
    ...


However, this give errors, and not works.


Regards,
Hollandz


Dario Concilio

unread,
Aug 10, 2015, 7:14:56 AM8/10/15
to django...@googlegroups.com
Hi again...
I could need to read this https://code.djangoproject.com/wiki/MultipleColumnPrimaryKeys
 
Bye
Dario
 

Date: Mon, 10 Aug 2015 02:39:12 -0700
From: holla...@gmail.com
To: django...@googlegroups.com
Subject: how to description FK which contains more than 1 field
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/0b01151a-9610-46d4-b74d-a985ee2da459%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Dario Concilio

unread,
Aug 10, 2015, 7:15:33 AM8/10/15
to django...@googlegroups.com
Hi hollandzwz,
try to change to_field in related_name="entitites"
 
Example:
 
class UserFollower(models.Model):
    user_id = models.ForeignKey(User,related_name="users")
    follower_id = models.ForeignKey(User,related_name="followers")
 
bye
Dario Concilio

 

Date: Mon, 10 Aug 2015 02:39:12 -0700
From: holla...@gmail.com
To: django...@googlegroups.com
Subject: how to description FK which contains more than 1 field
Reply all
Reply to author
Forward
0 new messages