How to link foreign keys & primary keys using python?

sonal sonaldgr8 at gmail.com
Fri Jun 9 14:37:48 CEST 2006


Hi all,
I hv started with python just recently... and have been assigned to
make an utility which would be used for data validations...
In short we take up various comma separated  data files
for eg: area.txt, school.txt, students.txt.... and so on (ok?!?)
now,
1. area code used in the school.txt must be defined in the area.txt
    (Primary key in area => area_code defined in area.txt
    & Foreign key on school => area_code defined in school.txt)

i hv created primary key using the following piece of code:

# primary key for area.txt (index created on the column AREACODE as per
the
# schema defined earlier )
area_pk = PartitionedPK( name     = 'Area PK ',
                                    save_to  =
'../Index/area_code.idx',
                                    fields   = ['A_AREACODE'] )
 # col name in area schema

# foreign key is defined as follows...
school_fk = HashedFK( name      = ' School code FK',
                               load_from = '../Index/area_code.idx',
                               fields    = ['S_AREACODE'] )
  # col name in school schema

Description for abv code:
1.  An index {area_code.idx } is formed on the field AREACODE in the
area.txt
     (i.e.,A_AREACODE)
2.  The data values in the S_AREACODE field in the school.txt are
checked in the index {area_code.idx}
    If the area code given in school.txt is not present in the area
code, then the record is not validated(as foreign key constraint
fails.)

Now if the Primary key is on mutiple columns...the foreign key, which
is also definedon the same no. of columns works..
for eg..

# primary key for school.txt (index created on the columns AREACODE &
SCHOOLCODE as per the
# schema defined earlier )
school_pk = PartitionedPK( name     = 'School PK ',
                                    save_to  = '../Index/school.idx',
                                    fields   =
['S_AREACODE','S_SCHOOLCODE'] )          # col names in school schema

# foreign key for students is defined as follows...
student_fk = HashedFK( name      = ' STUDENT code FK',
                               load_from = '../Index/student.idx',
                               fields    =
['STUD_AREACODE','STUD_SCHOOLCODE'] )               # col name in
student schema

Now if I hv to define foreign key on student.txt but with only one
field, school code, so as to make sure that the school name given in
the student.txt exists in the school.txt whatever be the area code...
I am unable to do this...

Say, if the AREACODE field is not present in the student.txt file....
then???
I tried using the foll code

student_fk = HashedFK( name      = ' Student FK',
                               load_from = '../Index/school.idx',
                               fields    = ['STUD_SCHOOLCODE'] )

Its showing an AttributeError : 'list' object has no attribute
'has_key'

I also tried making another index with only SCHOOLCODE field (another
PK for school.txt) and the foreign key on student.txt to be loaded from
this index...
it still shows the same error
That's may be becoz... one SCHOOLCODE may repeat often, with a
different AREACODE... so may be we need to index on distinct
SCHOOLCODES in school.txt...
how do i do that??

I hope i am not confusing the genious' ... plz help me...
till get my hands on python... :)




More information about the Python-list mailing list