Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Ticket #1139 (closed defect: fixed)

Opened 13 years ago

Last modified 12 years ago

[PATCH] SQLObject bug in handling of non default foreign keys

Reported by: trlandet Owned by: anonymous
Priority: normal Milestone: 1.0b2
Component: SQLObject Version: 1.0b1
Severity: normal Keywords: regression, patch
Cc:

Description

I have 3 classes:

class Base(InheritableSQLObject):
   ...
class File(SQLObject):
   ...
class MyClass(Base):
   aFile = ForeignKey("File")
   otherFile = ForeignKey("File", default=None)

Note: I have renamed classes etc, but as far as I can see this is the exact same setup as I have in developement, only with the irrelevant stuff removed

I then try to create an object:

object = MyClass(aFile=fileObject1, otherFile=fileObject2)

This did work in 0.9a8 but fails in 1.0b1 with the following exception

...
File ".....\SQLObject-0.7.1dev_r1860-py2.4.egg\sqlobject\inheritance\__init__.py", line 219, in _create
    raise TypeError, "%s() did not get expected keyword argument %s" % (self.__class__.__name__, col.name)
TypeError: MyClass() did not get expected keyword argument aFileID

The fix is to write:

object = MyClass(aFileID=fileObject1.id, otherFile=fileObject2)

And this works as expected, also for otherFile (!) when the only difference is that otherfile is not a required attribute.

Setup notice:

Base and MyClass are in the same .py file but File is in another file. My model.py looks like this:

from turbogears.database import PackageHub
from Model import __all__ as dbClasses
from Model import *

hub = PackageHub("turbogearspackage")
__connection__ = hub


# Connect the database classes to the
# database through TurboGears
for c in dbClasses:
    # locals contain all local variables, including the imported classes
    # Connect all classes to the database defined in the config files
    locals()[c]._connection = hub

SQLObject creates the right (PostgreSQL) tables with foreign key restraints, MyClass.sqlmeta.columns has aFile as a foreign reference to the right table etc.

Attachments

case.py Download (1.4 KB) - added by trlandet 13 years ago.
This short script shows the bug

Change History

comment:1 Changed 13 years ago by jorge.vargas

  • Severity changed from critical to normal

this may be related to the upgrade done in 0.9a9 of the SO project. http://trac.turbogears.org/turbogears/browser/tags/0.9a9/CHANGELOG.txt

InheritableSQLObject has had some problems with 3 levels of inheritance and maybe one of the bug fixes made your code break

I suggest that you look for the changes  http://svn.sqlobject.org/SQLObject/branches/0.7-bugfix/sqlobject/

svn log -r1457:1869  http://svn.sqlobject.org/SQLObject/branches/0.7-bugfix/sqlobject/inheritance/__init__.py


r1747 | phd | 2006-05-02 10:49:00 -0400 (Tue, 02 May 2006) | 3 lines

A patch by Konrad Zielinski <kzielinski@…> - test all required keywords before creating parent object(s).


not sure if that one above broke your code

Changed 13 years ago by trlandet

This short script shows the bug

comment:2 Changed 13 years ago by trlandet

  • Keywords regression, patch added; regression removed
  • Summary changed from SQLObject Regression in 1.0b1 from 0.9a8 to [PATCH] SQLObject bug in handling of non default foreign keys
  • Milestone changed from 1.0 to 1.0b2

A patch for this bug:

change the file sqlobject.inheritance.init line 218

from:

if col._default == sqlbuilder.NoDefault and col.name not in kw:

to:

if col._default == sqlbuilder.NoDefault and (col.name not in kw and col.foreignName not in kw):

This fixes the problem of foreign keys with non default values in SQLObject. Patch also submited to SQLObject at  Sourceforge

Please apply this fix,

Tormod Ravnanger Landet

comment:3 Changed 13 years ago by trlandet

  • Milestone changed from 1.0b2 to 1.0

Sorry, a bit quick there... it should of course be something like this

if col._default == sqlbuilder.NoDefault and col.name not in kw and (col.foreignName is None or col.foreignName not in kw):

Not all required keys are foreign keys

comment:4 Changed 13 years ago by trlandet

  • Milestone changed from 1.0 to 1.0b2

comment:5 Changed 13 years ago by trlandet

  • Status changed from new to closed
  • Resolution set to fixed

Fixed upstream in SQLObject

Note: See TracTickets for help on using tickets.