On Nov 9, 2010, at 8:47 AM, yannick wrote:
> The following simple code does not seem to produce the ALTER statement
> expected for the Contact.ownername column.
> When I look at the DB, there is no ForeignKeyConstraint on
> Contact.ownername.
SQLite doesn't support ALTER on an existing Column construct. use_alter=True
is a noop when using SQLite. This was definitely written out somewhere at
some point but doesn't appear to be in current documentation.
The error message you are receiving is due to the missing value for the
"ownername" column when the INSERT for Contact proceeds. post_update=True
means a second UPDATE statement will be emitted to populate this column, so the
column here cannot be nullable with that setting; however post_update=True is
not needed for that relationship, its only needed on one of the mutually
dependent relationships.
SQLite does not enforce foreign keys by default in any case, unless you issue
"PRAGMA foreign_keys=true".
>
> I think this results in problems later on: when I activate the
> relationships, I cannot do
> v=User(username="v")
> session.add(v)
> session.commit()
> c2=Contact(contactname="c2",owner=v)
> session.add(c2)
> session.commit()
> sqlalchemy.exc.IntegrityError: (IntegrityError) contact.ownername may
> not be NULL u'INSERT INTO contact (contactname, ownername) VALUES
> (?, ?)' ('c2', None)
> which doesn't seem normal.
>
>
> Here is the code:
>
> from datetime import datetime
> from sqlalchemy import (create_engine, MetaData, Table, Column,
> Integer,
> String, DateTime, Float, ForeignKey, and_, select)
> from sqlalchemy.orm import mapper, relationship, create_session,
> column_property,sessionmaker
> from sqlalchemy.ext.associationproxy import AssociationProxy
> from sqlalchemy.ext.declarative import declarative_base
>
> engine = create_engine('sqlite://', echo=True)
> #engine = create_engine('sqlite:///test.db', echo=True)
> metadata = MetaData(engine)
> Session = sessionmaker(bind=engine)
> session=Session()
>
> Base = declarative_base(bind=engine, metadata=metadata)
>
> class User(Base):
> __tablename__ = 'user'
> username = Column(String(50), primary_key=True)
> contactname = Column(String(50),
> ForeignKey('contact.contactname'), nullable=True)
>
>
> class Contact(Base):
> __tablename__ = 'contact'
> contactname = Column(String(50), primary_key=True)
> ownername = Column(String(50), ForeignKey('user.username',
> use_alter=True, name="fk_owner_constraint"), nullable=False)
> # contacted_users = relationship("User", backref="contact",
> post_update=True, primaryjoin=contactname==User.contactname)
> # owner = relationship("User", backref="owned_contacts",
> post_update=True, primaryjoin=User.username==ownername)
>
> metadata.create_all(engine)
>
> --
> You received this message because you are subscribed to the Google Groups
> "sqlalchemy" group.
> To post to this group, send email to [email protected].
> 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.
>
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected].
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.