Consider the following code:

from sqlobject import *
from sqlobject.inheritance import InheritableSQLObject

__connection__ = "sqlite:/:memory:"

def default_morx():
    deriveds = list(Derived.select())
    if deriveds.count():
        return deriveds[0].morx
    else:
        return -1

class Base(InheritableSQLObject):
    pass

class Derived(Base):
    morx = IntCol(default=default_morx)


Base.createTable()
Derived.createTable()

d = Derived()

---

It tries to create a table class with a reasonable default value for a
column.  But it raises an error because when default_morx is called,
there is a half-constructed object hanging aroung.  Look at the debug
output (of a version in which default_morx doesn't do any database
access and simply returns 1):


 3/QueryIns:  INSERT INTO base (child_name) VALUES ('Derived')
 3/QueryR  :  INSERT INTO base (child_name) VALUES ('Derived')
 4/QueryOne:  SELECT child_name FROM base WHERE id = (1)
 4/QueryR  :  SELECT child_name FROM base WHERE id = (1)

[called default_morx here]

 5/QueryIns:  INSERT INTO derived (id, morx, child_name) VALUES (1, 1,
NULL)
 5/QueryR  :  INSERT INTO derived (id, morx, child_name) VALUES (1, 1,
NULL)
 6/QueryOne:  SELECT morx, child_name FROM derived WHERE id = (1)
 6/QueryR  :  SELECT morx, child_name FROM derived WHERE id = (1)

Notice that there is a row in base at the time default_morx is called,
but not a corresponding row in derived.

There are two ways I can see to deal with this:

1. Wait to create the row in base until just before the row in derived
is to be created.

2. (maybe) use transactions.

3. Have a is_fully_created boolean on every base element, and filter on
that -- then set it when done creating. 


-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
sqlobject-discuss mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sqlobject-discuss

Reply via email to