#70 __connection__.style broken in .6.1

General (125)
Jonathan Ellis

I created a Style subclass but it didn't work. So I
changed all my Style's methods to raise an exception.
Still showed default behavior. Then I tried assigning
_style in each class -- that worked. I'd very much
like to be able to set it globally, though.


  • Oleg Broytman
    Oleg Broytman

    Logged In: YES

    Create a class that will be served as the base class for all
    your tables. Assign your style to it:

    class Base(SQLObject):
    _style = MyStyle()

    class MyTable(Base):

  • Logged In: NO

    That is a nice workaround, but either dbconnection.style
    should work or it should be removed...

  • Logged In: YES

    I have encountered this problem as well (trying to use long
    IDs) and I think I have figured out why it happens. When
    you create a table class, it is a subclass of SQLObject and
    hence an instance of MetaSQLObject. The __new__ method of
    MetaSQLObject sets _connection to __connection__ if it is
    unset, and then sets _style to _connection.style or
    styles.defaultStyle if it is unset. This sounds good,

    The trick is that SQLObject itself is an instance of
    MetaSQLObject, so when sqlobject is *loaded*,
    SQLObject._style gets set to styles.defaultStyle. Nothing
    can be done about this, since you haven't had a chance to
    set __connection__ yet, let alone __connection__.style.
    Then when you create table classes, since they are
    subclasses of SQLObject, they inherit its _style attribute,
    so MetaSQLObject.__new__ doesn't even look at _connection.style.

    An even easier version of the workaround below, as long as
    you are only using one style in your entire program, is to
    set SQLObject._style to your desired style. This obviates
    the need for an extra base class.

    I think it would be best if style could be set by
    connection, since the style used is really a property of the
    database in question. I'm not sure how one would accomplish
    this, however, if the above is indeed the problem. Maybe
    MetaSQLObject.__new__ should decline to set _style if it is
    not setting _connection as well?

  • Oleg Broytman
    Oleg Broytman

    • status: open --> closed-wont-fix