[Grok-dev] [MeGrok & SqlAlchemy] Simple relationship 1:1 fails with > "Foreign key assocated with column ------- could not find table" > (Hector Blanco)

Hector Blanco white.lists at gmail.com
Mon Nov 1 15:39:31 EDT 2010


Hi Katy and rest of the list...

I think I got it!

Investigating the "metadata" instance (printing the contents of
metadata.__dict__), I realized that in it appear references to the
tables although not as "table_name" but "schema_name.table_name".
Let's say my "schema" (in MySQL terminology) is called "test":
The following line:
   id = Column("id", Integer, ForeignKey("parent_table.id"), primary_key = True)
fails, but this:
   id = Column("id", Integer, ForeignKey("test.parent_table.id"),
primary_key = True)
works fine.

If I do:
    for fieldKey, fieldVal in metadata.__dict__.iteritems():
        print("::engine_created > field metadata." + str(fieldKey) +
"== " + str(fieldVal))

I get (among other things) this:
::engine_created > field metadata.tables == {'test.children_table':
Table('children_table', MetaData(None), Column('id', Integer(),
ForeignKey('test.parents_table.id'), table=<children_table>,
primary_key=True, nullable=False), Column('type', String(length=2,
convert_unicode=False, assert_unicode=None, unicode_error=None,
_warn_on_bytestring=False), table=<children_table>), schema='test'),
'test.parents_table': Table('parents_table', MetaData(None),
Column('id', Integer(), table=<parents_table>, primary_key=True,
nullable=False), Column('whatever_field', String(length=16,
convert_unicode=False, assert_unicode=None, unicode_error=None,
_warn_on_bytestring=False), table=<parents_table>), schema='test')}

As you can see, the parent table appears as "test.parents_table", not
"parents_table".

I hope this helps to someone else

2010/10/30 Kathy Manwaring <kathy at perfectnotes.com.au>:
> Hi Hector,
>
> Someone may have pointed this out already, but you have 'parent_table' in
> the Child.py file, but 'parents_table' everywhere else...
> I know this is not what the error message says, but it is probably part of
> the problem.
>
> Hope this helps!
> Kathy
>
> 0406 911 985
> kathy at perfectnotes.com.au
> Perfect Notes I. T.
>
>> Date: Fri, 29 Oct 2010 18:44:04 -0400
>> From: Hector Blanco <white.lists at gmail.com>
>> Subject: [Grok-dev] [MeGrok & SqlAlchemy] Simple relationship 1:1
>>       fails with "Foreign key assocated with column ------- could not find
>>       table"
>> To: grok-dev at zope.org
>> Message-ID:
>>       <AANLkTim4x8L0XQ18G6fZ-gBpbRNGFbYp_LgE30X9uQ+9 at mail.gmail.com>
>> Content-Type: text/plain; charset=ISO-8859-1
>>
>> Hello, list!
>>
> <snip>
>> I am getting this error:
>> Foreign key assocated with column 'children_table.id' could not find
>> table 'parents_table' with which to generate a foreign key to target
>> column 'id'
>>
>> I have a file, called Tables.py where all the classes and auxiliary
>> (or intermediate) tables that I'm going to use in my application are
>> defined:
>>
>> Tables.py >>
>>
>> class Parent(rdb.Model):
>>       rdb.metadata(metadata)
>>       rdb.tablename("parents_table")
>>
>>       id = Column("id", Integer, primary_key=True)
>>       _whateverField= Column("whatever_field", String(16)) #Irrelevant
>>
>>       child1 = relationship("Child", uselist=False)
>>
>> class Child(rdb.Model):
>>       rdb.metadata(metadata)
>>       rdb.tablename("children_table")
>>       id = Column("id", Integer, ForeignKey(Parent.id), primary_key = True)
>>       type = Column("type", String(2)) #Irrelevant (for this example)
>>
>> #A few lines below, said classes are "Grokified":
>>
>> def setComponents():
>>       """Grok all the classes related to the database"""
>>       grok_component("Parent", Parent)
>>       grok_component("Child", Child)
>>       print "Grokified!"
>>
>> #This method is properly run... I see a "Grokified" message properly.
>>
>>      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
>> -
>> And then I have two different Python .py files (Parent.py and
>> Child.py) where the methods that manage said classes are implemented.
>> In those files, the static area of each class is copied from Tables.py
>> with some changes in the quotes (where I can use the object itself, I
>> use it):
>>
>> Parent.py >>
>>
>> from child import Child
>> metadata = rdb.MetaData()
>>
>> class Parent(rdb.Model):
>>       rdb.metadata(metadata)
>>       rdb.tablename("parents_table")
>>
>>       id = Column("id", Integer, primary_key=True)
>>       _whateverField= Column("whatever_field", String(16)) #Irrelevant
>>
>>       child1 = relationship(Child, uselist=False) #No quotation marks on this
>> Child
>>      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
>> -
>> And
>>
>> Child.py > >
>> metadata = rdb.MetaData()
>>
>> class Child(rdb.Model):
>>       rdb.metadata(metadata)
>>       rdb.tablename("children_table")
>
> THIS NEXT LINE SEEMS TO HAVE THE WRONG NAME:
>
>>       id = Column("id", Integer, ForeignKey("parent_table.id"), primary_key =
>> True)
>>       type = Column("type", String(2)) #Irrelevant (for this example)
>>      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
>> -
>>
>> When I try to use these classes, I get:
>> Foreign key assocated with column 'children_table.id' could not find
>> table 'parents_table' with which to generate a foreign key to target
>> column 'id'
>>
>> But if I take a look to the tables with a MySQL Query Browser, the
>> table "parents_table" is there, happily and properly created.
>>
>> In some other places, I have had similar problems, but I've been able
>> to fix them by delaying the imports. I had been able to (kind of)
>> import the Parent type in the Child file so I can use the Parent
>> object directly. It would be a little bit as if in this case I was
>> able to do:
>>
>> from parent import Parent
>> [ . . . ]
>>
>> class Child(rdb.Model):
>>       [ . . . ]
>>       id = Column("id", Integer, ForeignKey(Parent.id), primary_key = True)
>>
>> and that usually fixed the problem but in this specific case, I can't
>> really do that: In the Parent file I need to import the Child and that
>> gives a very, very nasty circular dependency problem.
>>
>> Is there a way to tell the Child.py file something like "Hey, dude...
>> Here's the parent_table that you need!" ?  (Well... In a more Pythonic
>> way, of course... I don't think 'dude'is a reserved keywork in Python,
>> or an SqlAlchemy type). I don't know, something like:
>>
>> from whathever.repository.of.tables import parent_table
>>
>>          so I can, without quotes, use:
>>
>> id = Column("id", Integer, ForeignKey(parent_table.id), primary_key =
>> True)
>>
>> (I guess that may work)
>>
>> I tried to figure out something out of these links:
>> http://pypi.python.org/pypi/megrok.rdb (basic documentation)
>> http://grok.zope.org/documentation/how-to/orm-using-megrok.rdb-and-sqlalchemy
>>  (more advanced)
>>
>> but I still wasn't able to get anything. If anyone knows a good
>> megrok.rdb tutorial, that would be very helpful as well...
>> So I don't have to ask that much :-(
>> "don't give a needy person a fish, give him a fish pole and teach him
>> how to fish" ... type :-)
>>
>> Thank you!!
>>
>>
>> ------------------------------
>
>
> _______________________________________________
> Grok-dev mailing list
> Grok-dev at zope.org
> https://mail.zope.org/mailman/listinfo/grok-dev
>


More information about the Grok-dev mailing list