[Modeling-cvs] ProjectModeling/Modeling/doc/UserGuide DefiningaModel.tex,1.35,1.36
Status: Abandoned
Brought to you by:
sbigaret
|
From: <sbi...@us...> - 2003-08-31 17:22:54
|
Update of /cvsroot/modeling/ProjectModeling/Modeling/doc/UserGuide
In directory sc8-pr-cvs1:/tmp/cvs-serv16069/Modeling/doc/UserGuide
Modified Files:
DefiningaModel.tex
Log Message:
Added doc.: how to model many-to-many relationships (to be continued)
Index: DefiningaModel.tex
===================================================================
RCS file: /cvsroot/modeling/ProjectModeling/Modeling/doc/UserGuide/DefiningaModel.tex,v
retrieving revision 1.35
retrieving revision 1.36
diff -C2 -d -r1.35 -r1.36
*** DefiningaModel.tex 30 Aug 2003 16:46:49 -0000 1.35
--- DefiningaModel.tex 31 Aug 2003 17:22:50 -0000 1.36
***************
*** 1853,1856 ****
--- 1853,2008 ----
+ \subsection{Modeling many-to-many relationships\label{design-rels-many-to-many}}
+
+ Many-to-many relationships are not automatically handled by the framework yet,
+ however they can be modeled and used as such.
+
+ We'll first examine how many-to-many relationships are mapped in relational
+ database schema. Then we will give a short example demonstrating how to do
+ this, including the code that you'll have to insert in your classes to handle
+ them.
+
+ We will use the following model as an example:
+ \begin{verbatim}
+ +--------+ 0,* 0,* +---------+
+ | Person |<-persons------addresses-->| Address |
+ |--------| |---------|
+ | name | | street |
+ +--------+ | town |
+ +---------+
+ \end{verbatim}
+
+ \subsubsection{General principle: the correlation table\label{design-rels-many-to-many-principle}}
+
+ Modeling many-to-many relationships in a relational database schema usually
+ involves designing a \emph{correlation table}, a table which holds the
+ informations about the objects in the two tables, \class{Person} and
+ \class{Address}. Each line in the correlation table is basically a tuple of
+ two elements, one from \class{Address} and one from \class{Person}, so that we
+ can tell that if \code{person_1} and \code{address_2} appear in a single row
+ of the correlation table, we know that those two objects are related to each
+ other.
+
+ Now let's integrate the correlation table into our model:
+ \begin{verbatim}
+ +--------+ 0,1 0,* +----------+ 0,* 0,1 +---------+
+ | Person |<-person---pAs->>| PersAddr |<<-pAs------address-->| Address |
+ |--------| |----------| |---------|
+ | name | | FKperson | | street |
+ +--------+ | FKaddress| | town |
+ +----------+ +---------+
+ \end{verbatim}
+
+ where:
+ \begin{itemize}
+
+ \item \class{PersAddr} is the abbreviation for PersonAddress (the correlation
+ table is usually named after the names of the two tables it correlates)
+
+ \item relationships named \code{personAddresses} are abbreviated with
+ \code{pAs}
+
+ \end{itemize}
+
+ To fully understand what's going on here, let's look at the tables themselves.
+ Suppose tables \class{Address} and \class{Person} have 3 rows each:
+ \begin{verbatim}
+ SELECT * FROM Person;
+ id | name
+ --- +------
+ 1 | p1
+ 2 | p2
+ 3 | p3
+
+ SELECT * FROM Address;
+ id | street | town
+ ----+-----------+------
+ 1 | street a1 |
+ 2 | street a2 |
+ 3 | street a3 |
+ \end{verbatim}
+
+ Then, for the following situation:
+ \begin{itemize}
+ \item \code{p1}'s addresses are \code{[a1, a2]}
+ \item \code{p2}'s addresses are \code{[a1]}
+ \item \code{p3}'s addresses are \code{[a1, a3]}
+ \end{itemize}
+
+ The correlation table looks like:
+
+ \begin{verbatim}
+ SELECT * FROM Person_Address;
+ id | fk_person | fk_address
+ ----+-----------+------------
+ 1 | 1 | 1 # p1 <--> a1
+ 2 | 1 | 2 # p1 <--> a2
+ 3 | 2 | 1 # p2 <--> a1
+ 4 | 3 | 3 # p3 <--> a3
+ 5 | 3 | 1 # p3 <--> a1
+ \end{verbatim}
+
+ \subsubsection{A short example\label{design-rels-many-to-many-principles}}
+
+ Now that we now how many-to-many relationships are handled, we know what we
+ should do:
+
+ \begin{enumerate}
+
+ \item define the correlation table as an entity in the model,
+
+ \item connect the two tables to the correlation table and back,
+
+ \item because we do not want to directly manipulate objects coming from the
+ correlation table, we will write some code so that we can directly
+ access persons from a given address, and addresses from a given person;
+
+ \end{enumerate}
+
+ We've seen in the previous how the model looks like with the correlation
+ table. Here is the full PyModel:
+
+ \makeatletter
+ \def\verbatim@font{\footnotesize\ttfamily}
+ \makeatother
+ \begin{verbatim}
+ from Modeling.PyModel import *
+
+ # defaults
+ AString.defaults['width'] = 40
+ Entity.defaults['properties'] = [
+ APrimaryKey('id', isClassProperty=0, isRequired=1, doc='PK')
+ ]
+
+ ##
+ _connDict = {'database': 'MM1', 'user': 'postgres', 'host': 'localhost',
+ 'password': ''}
+ model = Model('MM1',adaptorName='Postgresql',connDict=_connDict)
+ model.version='0.1'
+ model.entities = [
+ Entity('Person',
+ properties=[ AString('name',isRequired=1) ] ),
+ Entity('Address',
+ properties=[ AString('street', isRequired=1),
+ AString('town') ] ),
+ Entity('PersonAddress'),
+ ]
+ #---
+ model.associations=[
+ Association('PersonAddress','Person',
+ relations=['person','personAddresses'],
+ delete=['nullify','cascade'] ),
+ Association('PersonAddress','Address',
+ relations=['address','personAddresses'],
+ delete=['nullify','deny'] ),
+ ]
+ \end{verbatim}
+
+
+
+
+ \makeatletter
+ \def\verbatim@font{\normalsize\ttfamily}
+ \makeatother
|