From: F. GEIGER <fgeiger@da...> - 2001-05-03 17:21:57
I redesigned my database. Until today I had only one table, not normalized,
it was only to have something to play with. Now I made 4 tables out of it,
which are connected by id values. The main table is "Produkt_T", which
contains besides others a column "Marke_TId", that points to the table
So i have the "problem" of having to join queries (FYI: "marke" means
"brand", "marken" means "brands", "produkte" means "prodcts", "_T" means
"it's a table", "_VC" means "varchar"):
# Search for 'marke', which e.g. is "Faber"
marken = store.fetchObjectsOfClass('Marke_T', clauses="WHERE Name_VC LIKE
'%s%%'" % marke)
assert len(marken) == 1, "len(marken) == 1"
# Now we have a 'marke', where marke.serialNum() equals to 2
marke = marken # Just to be sure...
print "Serial # =", marke.serialNum() # Prints the value '2'
# produkte = store.fetchObjectsOfClass('Produkt_T', clauses="WHERE
Marke_TId=%d" % marke.serialNum())
##### Does not work because Marke_TId in Produkt_T equals to
9328975928786727 or something like that
produkte = store.fetchObjectsOfClass('Produkt_T') # Not good, it yields
the *whole* table
# Now we *could* scan thru the 'produkte' and find the one with
# produkt.marke_TId().serialNum() == marke.serialNum()
Id's in "Produkt _T", e.g. "Marke_TIds" (which points to "Marke_T"), aren't
id's but references to objects, I guess.
So two questions arise:
1) How can I achieve what I intended to do?
2) How is Webware able to find and create the objects ref'ed by bigint's
like 897658726582765. I did not find them elsewere in the database.
Concerning (1): Or am I on the wrong way at all? If so, how would you do it?
At 07:23 PM 5/3/2001 +0200, F. GEIGER wrote:
>So two questions arise:
>1) How can I achieve what I intended to do?
>2) How is Webware able to find and create the objects ref'ed by bigint's
>like 897658726582765. I did not find them elsewere in the database.
>Concerning (1): Or am I on the wrong way at all? If so, how would you do it?
I haven't totally grokked your message, as it's late and my brain is full,
but I think I have seen enough to help you some tonight.
For objects that are connected, you should haven't to do any SQL clauses at
all. The SQL clause feature is provided mostly so you can filter out
certain objects, or sort them on the server side.
So if you have an "obj ref" type, then you just use it:
If you have a 1-m relationship, you just use that too:
department.employees() # returns a list
Under the hood, MK takes care of doing the queries. Those methods are
generated for you.
If you want all employees, you can do this:
emps = store.fetchObjectsOfClass('Employee')
You could also pass the actual Python class instead of a string. Your choice.
If you wanted only the ones that started with 'A' you can do this:
empsA = store.fetchObjectsOfClass('Employee', clauses="where
lastName like 'A%')
So stop thinking about doing the joins yourself. MK tries to give you an
object model with "references" and "lists". The joins are supposed to
happen behind the scenes.
See how far you get with that and re-review the Quick Start Guide. That
should get you further along and then we can go from there.