i've attached an example, where a class constructor setups a default value
on a relation field, in the constructor, the app code latter explicitly sets
the fk attribute on the class, but sa ignores this value, and in the
flushing process sets it be the value of the orm field, so that it
'magically' becomes none even though it was explicitly set, which throws an
integrity constraint violation since the fk is setup to be not null.
not sure if this a bug or just a warning on sa usage patterns..

cheers,

kapil

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

from sqlalchemy import create_engine, MetaData, Table, Column, types, ForeignKey
from sqlalchemy.orm import mapper, session, relation

metadata = MetaData()
metadata.bind = create_engine('sqlite://')

model_table = Table("models",
                    metadata,
                    Column("id", types.Integer, primary_key=True),
                    Column("status_id", types.Integer, ForeignKey("statuses.id"), nullable=False),                    
                    )

status_table = Table("statuses",
                     metadata,
                     Column("id", types.Integer, primary_key=True),
                     Column("name", types.Unicode, unique=True )
                     )

metadata.create_all()

class Model( object ):

    def __init__( self, status=None):
        self.status = None
        
class Status( object ): pass

mapper( Model, model_table,
        properties = {
           'status': relation( Status, backref="models")
           }
        )

mapper( Status, status_table )

status_table.insert( values=dict(id=1, name=u'production') ).execute()

s = session.Session()

m = Model()
m.status_id = 1

s.save(m)
# nice error .. integrity constraint violation,  message status_id is null magically
s.flush()


    









        
    

Reply via email to