From: Zoran B. <Zor...@sl...> - 2009-01-08 11:19:46
|
I would like to have separate tables. Example that I gave was simplified. In reality each block might have more parents and each block might also have more childs. So separate connection table will be easier to handle in application (when adding or deleting connections between blocks). Zoran ________________________________ From: Petr Jakeš [mailto:pet...@tp...] Sent: Thursday, January 08, 2009 11:32 AM To: Zoran Bošnjak Cc: sqlobject-discuss Subject: Re: [SQLObject] FW: table ralations So what about one table only? (main idea taken from http://wiki.sqlobject.org/joinexample.html) sqlhub.processConnection = connectionForURI('sqlite:/:memory:') class Blocks(SQLObject): name=StringCol() parent=ForeignKey("Blocks", default=None) child=SingleJoin("Blocks", joinColumn="parent_id") Blocks.createTable() blockA = Blocks(name="blockA",) blockB = Blocks(name="blockB", parent=blockA) blockC = Blocks(name="blockC", parent=blockB) for row in Blocks.select(): print "ROW:", row print "PARENT:", row.parent print "CHILD:", row.child Petr PS: please try to "reply to all" so others in the SQLobject discussion group can profit as well On Thu, Jan 8, 2009 at 10:53 AM, Zoran Bošnjak <Zor...@sl...> wrote: For example: The schema: BlockA --------> BlockB -------> BlockC We have 3 blocks and 2 connections. For this case the data in the table should be: Block table: id | name ----------- 1 | "BlockA" 2 | "BlockB" 3 | "BlockC" Connection table: id | src | dst --------------- 1 | 1 | 2 # BlockA -> BlockB connection 2 | 2 | 3 # BlockB -> BlockC connection ... 3 | 1 | 1 # this is connection from BlockA to itself, but this is OK (not shown on the schema above) 4 | 3 | 4 # wrong entry... it should not be possible to enter something like this in a database, block with id 4 is non-existant! Now, how should I declare classes for Block and Connection table? Each entry in a connection table should have id and src, which should be a reference to an existing block, and dst, which should be again a reference to an existing block Thank you for your answer. Zoran ________________________________ From: pet...@gm... [mailto:pet...@gm...] On Behalf Of Petr Jakeš Sent: Thursday, January 08, 2009 8:39 AM To: Zoran Bošnjak Subject: Re: FW: [SQLObject] table ralations Zoran, I do not really understand, what you are trying to do. Can you send me a short example of data for both tables? Petr On Thu, Jan 8, 2009 at 8:36 AM, Zoran Bošnjak <Zor...@sl...> wrote: Petr, do you understand my question. Can you help me solve it? regards, Zoran ________________________________ From: Zoran Bošnjak Sent: Wednesday, January 07, 2009 10:17 AM To: 'pet...@tp...' Cc: sql...@li... Subject: RE: [SQLObject] table ralations Sorry for the confusion... I don't want to have a "Connection" in a database unless the connection is between existing "Blocks". Please correct my class definition for Block and Connection so, that it won't be even possible to create such connection. Zoran ________________________________ From: pet...@gm... [mailto:pet...@gm...] On Behalf Of Petr Jakeš Sent: Wednesday, January 07, 2009 9:51 AM To: Zoran Bošnjak Cc: sql...@li... Subject: Re: [SQLObject] table ralations # create dumb connection # it should not work, because there is no blocks!!! Connection(src=150, dst=250) Why do you think this should not work? AFIK above mentioned just insert one row (record) in your table Connection. Exactly as the SQL below describes. INSERT INTO "connection" VALUES(1,150,250); COMMIT; sqlite> Petr |