Hello,
I'm trying to understand how SQLObject works, but I'm having problem
understanding the generated tables:
If I want to create two tables, with one that has a relation one-to-many to
the other, I define an attribute with the ForeignKey() method, but when I do
this, I notice that there are no *real* restriction on the usage of that
generated column in the database (I'm using PostgreSQL). Here are more
explanations:
Let's take two example tables/classes:
=======
class societe(SQLObject):
_connection = conn
_columns=[
StringCol('socNom', length=20, alternateID=True,
notNull=1,unique=True),
StringCol('socMail', length=50, unique=True),
]
secteurRegies = MultipleJoin('secteurRegie')
class secteurRegie(SQLObject):
_connection = conn
_columns=[]
societe = ForeignKey('societe')
societe.createTable()
secteurRegie.createTable()
=======
Here are the created tables in PostgreSQL:
Query : CREATE TABLE societe (
id SERIAL PRIMARY KEY,
soc_nom VARCHAR(20) NOT NULL UNIQUE,
soc_mail VARCHAR(50) UNIQUE,
)
Query : CREATE TABLE secteur_regie (
id SERIAL PRIMARY KEY,
societe_id INT
)
=======
And nothing more ! I would have expected that SQLObject would define
societe_id to be a foreign key to societe.id.
That means I can do INSERTs in secteur_regie with non-existent societe_id
values. That bothers me a lot.
I guess this is not too important if I'm using *only* SQLObject, but I'm
accessing/INSERTing data from other sources programs, and I want to be sure
that I don't have any incoherent data (and, btw, use the 'ON DELETE CASCADE'
to remove automatically deleted records).
So, to summarize, Is it possible to implement a code like this:
ALTER TABLE secteur_regie
ADD FOREIGN KEY (societe_id)
REFERENCES societe (id) ON UPDATE CASCADE ON DELETE CASCADE;
Or maybe I haven't understood how to declare foreign keys?
Thanks for your help,
Luc Stepniewski
--
Luc Stepniewski <ls...@ad...> <http://lstep.free.fr/>
Adelux - Securite, Linux Public key: <http://lstep.free.fr/pubkey.txt>
Key BC0E3C2A fingerprint = A4FA466C68D27E46B427 07D083ED6340BC0E3C2A
|