Hi! How are you doing today?<br><br>I'm writing because I have a database problem. I am trying to migrate a Grok/Zope application from ZopeDB to MySql, and I don't know exactly how to specify one of the relationships between tables... I am using “megrok.rdb” and “sqlalchemy”.<br>
<br>I have four classes involved:<br><ol><li>A record with some information (let's say name and address of people)</li><li>A class that can store records of the aforesaid type (it's basically a class that behaves as a list)</li>
<li>A “manager” that has some other information and two of instances of the class who stores entries (two lists)</li><li>An “external” (external to all this assembly) class that uses one of these managers.</li></ol><br>For the example, the external class looks like:<br>
<br><span style="font-family: courier new,monospace;">class External(rdb.Model):</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> rdb.metadata(metadata)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> rdb.tablename("externals")</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> id = Column("id", Integer, primary_key=True)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> title = Column("title", String(100))</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> moreStuff = Column("more_stuff", Integer(unsigned=True))</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> entryManager = relationship("EntryManager", secondary="entry_managers"")</span><br><br>And then:<br><br><span style="font-family: courier new,monospace;">class Entry(rdb.Model):</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> rdb.metadata(metadata)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> rdb.tablename("entries")</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> id=Column("id", Integer, primary_key=True)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> name = Column("name", String(16))</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> address = Column("address", String(16))</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> def __init__(self):</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> self.data = “hello”</span><br><br><span style="font-family: courier new,monospace;">class EntryContainer( ? ? ? ):</span><br style="font-family: courier new,monospace;">
<i><span style="font-family: courier new,monospace;"> #I really don't know very well what to do with this</span></i><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> def __init__(self):</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> self.__list = list()</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">class EntryManager(rdb.Model):</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> rdb.metadata(metadata)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> rdb.tablename("entry_managers")</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> id=Column("id", Integer, primary_key=True)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> myOtherStuff = Column("my_other_stuff", String(16))</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> containerA = EntryContainer()</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> containerB = EntryContainer()</span><br>
<br>My guess is that I don't really need the EntryContainer to be a<br>rdb.Model. It could just be an intermediate table of the type [id,<br>id_of_entry].<br><br>How can I tell to the system that containerA and containerB have to<br>
use that “intermediate” table?. I tried something like:<br><br><span style="font-family: courier new,monospace;">entries_container = Table(</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> "entries_container",</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> metadata,</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> Column("id", Integer, primary_key=True),</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> Column("entry_id", Integer, ForeignKey("<a href="http://backup_entries.id">backup_entries.id</a>"))</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">)</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">class EntryManager(rdb.Model):</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> rdb.metadata(metadata)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> rdb.tablename("entry_managers")</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> id=Column("id", Integer, primary_key=True)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> myOtherStuff = Column("my_other_stuff", String(16))</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> containerA = relationship("Entry", secondary="entries_container", backref="backup_entries")</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> containerB = relationship("Entry", secondary="entries_container", backref="backup_entries")</span><br style="font-family: courier new,monospace;">
<br>but I am not capable of using containerA and containerB as a regular<br>list() object... Something is failing. When I try to set up the tables<br>it says:<br><br><i style="font-family: courier new,monospace;">ArgumentError: Could not determine join condition between parent/child<br>
tables on relationship EntryManager.containerA. Specify a<br>'primaryjoin' expression. If 'secondary' is present, 'secondaryjoin'<br>is needed as well.</i><br><br>Thank you very much<br>