I just committed revision 800 which makes the correct adjustment to properly add reflected foreign keys. However, as of yet i have not figured out a way to get the foreign keys back when using MySQL and I suspect there may not be one (table reflection is still only mysql5 compatible also...anyone want to give me a freebie on mysql3/4 ? oracle ? ) .

So while sqlite and postgres (and someday oracle, when i write reflection for it) should find their own foreign keys and allow your example to work, with mysql you need to add them explictly. I added an "append_item()" method to Column and made it so that code which explicitly adds foreign keys will cleanly replace any that that were automatically reflected so you can safely make them explicit in order to write database-neutral code, i.e.

        # fine for sqlite/postgres, wont work in mysql
        master = Table( "MASTER", engine, autoload=True)
        detail = Table("DETAIL", engine, autoload=True)
        j = join(master, detail)
        print str(j.onclause)

        # fine for sqlite/postgres/mysql
        master = Table( "MASTER", engine, autoload=True)
        detail = Table("DETAIL", engine, autoload=True)
        detail.c.master_id.append_item(ForeignKey("master.master_id"))
        j = join(master, detail)
        print str(j.onclause)


On Jan 8, 2006, at 10:15 AM, Murat Özsöyler wrote:

Hi,

When autoloading table metadata from sqlite database, I have confronted an error. Foreign key definition for column is not reflected in table's foreign_keys property, foreign_key property of the related contains a ForeignKey class reference though. This causes errors when creating associated mapper which also creates one to many relation regarding the problem ForeignKey. If one appends column.foreign key to the table.foreign_keys, mapper can be created as in the example below.

from sqlalchemy import *

engine = create_engine("sqlite://filename=trial.db")

master = Table( "MASTER", engine, autoload=True)
detail = Table("DETAIL", engine, autoload=True)
# DETAIL table contains a column definition like this:
#   MASTER_ID integer references MASTER(ID),

class Master(object):
   pass
  class Detail(object):
   pass
  detail.foreign_keys.append(detail.c.MASTER_ID.foreign_key)

# below command gives error
#   "Cant find any foreign key relationships between 'MASTER'
#   (<sqlalchemy.schema.Table object at 0x01FDBE30>) and 'DETAIL'
#   (<sqlalchemy.schema.Table object at 0x01FDBED0>)" if the line
#   above is commented
# on the other hand gives
#   "AttributeError: parent" if uncommented.
#
# If all is entered from the intepreter, the line below gives no
# error if the command above is uncommented but the details property
# is not created
#
Master.mapper = mapper(Master, master, properties={
       "details": relation(Detail, detail)
       }
   )
  Am I doing something wrong?

Regards,

Murat Ozsoyler



-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the web. DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
_______________________________________________
Sqlalchemy-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users



-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_idv37&alloc_id865&op=click
_______________________________________________
Sqlalchemy-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users

Reply via email to