1対1の関係を築きたい。出力が None になるのはなぜですか?
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, ForeignKey, Float
from sqlalchemy import Sequence
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, relationship, backref
engine = create_engine('sqlite:///:memory:')#, echo=True)
Session = sessionmaker(bind=engine)
Base = declarative_base()
class User(Base):
__tablename__ = 'user'
id = Column(Integer, Sequence('user_id_seq'), primary_key=True)
name = Column(String(50))
def __init__(self, name):
self.name = name
class Item(Base):
__tablename__ = 'item'
id = Column(String, primary_key=True)
value = Column(Float, default=0)
user_id = Column(Integer, ForeignKey(User.id))
user = relationship("User", backref=backref('itemA', uselist=False))
def __init__(self, id, value, user_id = None):
self.id = id
self.value = value
self.user_id = user_id
session = Session()
Base.metadata.create_all(engine)
ed_user = User('ed')
session.add(ed_user)
session.flush()
it1 = Item('it1', 1, user_id='ed')
session.add(it1)
session.flush()
print it1.user
print ed_user.itemA