http://grok.zope.org/documentation/how-to/orm-using-megrok.rdb-and-sqlalchemy

http://www.sqlalchemy.org/docs/

--
Jeffrey D Peterson
Webmaster
Crary Industries, Inc.
237 12th St NW
West Fargo, ND 58078
P: 701-499-5928
E: jeff.peter...@crary.com

> -----Original Message-----
> From: sqlalchemy@googlegroups.com [mailto:sqlalch...@googlegroups.com]
> On Behalf Of Hector Blanco
> Sent: Tuesday, October 26, 2010 5:40 PM
> To: sqlalchemy@googlegroups.com
> Subject: [sqlalchemy] Containers/collections with SQLAlchemy
> 
> Hi group!
> 
> I am trying to migrate my application from ZopeDB to MySql. I am using
> sqlalchemy under megrok.rdb.
> 
> I have a class which inherits from list() and has a few extra methods.
> Reading the chapter about custom collections in sqlalchemy
> (http://www.sqlalchemy.org/docs/orm/collections.html#custom-collection-
> implementations),
> I thought that I'd be great having that class as a custom collection
> (extending from list() but simulating a set(), where the items can't
> be repeated)
> 
> That class that extends from list, acts as a container of other
> classes (mmm... yeah, as all the collections do... pretty obvious). It
> is used (as an attribute) in other classes as well:
> 
> class MyEntry(object):
>       def __init__(self):
>               self.field1 = "field1"
>               self.field2 = "field2"
> 
> 
> class MyContainer(list):
>               __emulates__ = set
>               def __init__(self):
>                       super(MyContainer,self).__init__()
> 
>               def add(self, myEntry):
>                       if isinstance(myEntry, MyEntry):
>                               if not(myEntry in self):
>                                       super(MyContainer, self).append(myEntry)
> 
>       def getByField1(self, field1):
>               for element in self:
>                       if element.field1 == field1:
>                               return element
>                       return None
> #     [ ...
> #             more useful methods,
> #             overloading to make the list behave like a set,
> #             yada yada yada
> #     ...]
> 
> 
> class MyClass(object):
>       def __init__(self):
>               self.container1 = MyContainer()
>               self.container2 = MyContainer()
>               self.anotherField = "hello world"
>       def getContainer1():
>               return self.container1
>       def getContainer2():
>               return self.container2
> 
> I see clearly the "MyEntry" and (more or less clearly) "MyClass"
> classes modeled in tables.
> I also see clearly the intermediate table for MyContainer:
> 
> my_containers_table = Table(
>       "my_containers_table",
>       metadata,
>       Column("id", Integer,  primary_key=True),
>       Column("my_entry_id", Integer, ForeignKey("my_entries_table.id"))
> )
> 
> So, in the MyClass class, each of MyContainer() instances can have an
> id and when someone wants to retrieve the MyEntry() elements that are
> in container1 (to say so), the "my_containers_table" can be used as a
> middle table to get said MyEntries.
> 
> but I don't know how to link the MyContainer(list) object with
> my_containers_table (and from there with MyClass) :-(
> 
> I'm not even sure whether MyClass.container1 and MyClass.container2
> should be ForeignKeys or Relations.
> 
> How can I establish the relationship between MyClass.container1 or
> MyClass.container2 with "my_containers_table"?
> 
> On one hand, I want MyClass.container1 and MyClass.container2 to be
> foreign keys, but on the other, I want them to be instances of
> MyContainer(list)... And that's where I start banging my head on the
> wall :-)
> 
> In my mind (preferably before banging it against the wall) I see this
> schema:
> 
>  +---Entry----+
>  |     id = 1    |
>  |    "field1"   |     +----container1---+
>  |    "field2"   |      |    id = 10            |
>  +-------------+     |  foreign[0] = 1   |
>                             |  foreign[1] = 2   |        +----- myClass
> ----+
>  +---Entry---+    +----------------------+       |      id = 101
> |
>  |    id = 2     |                                             |
> anotherField        |
>  |    "field1"   |                                             |
> container1 = 10  |
>  |    "field2"   |     +----container2---+        | container2 = 20  |
>  +-------------+     |      id = 20          |         +---------------
> ------+
>                             |  foreign[0] = 3   |
>  +---Entry---+    +---------------------+
>  |    id = 3    |
>  |    "field1"  |
>  |    "field2"  |
>  +------------+
> 
> [I hope the Ascii thing is properly displayed]
> 
> When I want to get all what is in myClass.container1, the system
> should go to my_containers_table with the myClass.container1's id (10)
> and retrieve all the "MyEntries" (id=1 and id=2 in the example above)
> pointed by the ForeingKey of "my_containers_table". That's what I want
> the system to do. But that's not what it's doing.
> 
> Any tip will be deeply appreciated. Links to manuals,
> documentations... whatever (I'm a total newbie in sqlmyalchemy)
> 
> Thank you again!
> 
> --
> You received this message because you are subscribed to the Google
> Groups "sqlalchemy" group.
> To post to this group, send email to sqlalch...@googlegroups.com.
> To unsubscribe from this group, send email to
> sqlalchemy+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/sqlalchemy?hl=en.

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to