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.

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 sqlalch...@googlegroups.com.
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to