Reputation: 167
I am trying to store user oauth details of 3rd party sites, BaseConnection table is used to store basic information of the connection
class BaseConnection(Base):
__tablename__ = "BaseConnection"
id = Column(Integer, primary_key=True)
username = Column(Unicode(255),
ForeignKey('users.username'))
type = Column(Unicode(255))
is_active = Column(Boolean, default=False)
last_connection = Column(DateTime, onupdate=datetime.utcnow)
__mapper_args__ = {
'polymorphic_identity': 'BaseConnection',
'polymorphic_on': type
}
def __init__(self, username, type, is_active):
self.username = username
self.type = type
self.is_active = is_active
Twitter connection is used to store ouath details of twitter account
class TwitterConnection(Base):
__tablename__ = "TwitterConnection"
connection_id = Column(Integer, ForeignKey('BaseConnection.id'),
primary_key=True)
uid = Column(Unicode(255))
access_key = Column(Unicode(255))
access_secret = Column(Unicode(255))
twitter_username = Column(Unicode(255))
refresh_date = Column(DateTime)
__mapper_args__ = {
'polymorphic_identity': 'TwitterConnection'
}
def __init__(self, connection_id,
uid, access_key, access_secret, twitter_username):
self.connection_id = connection_id
self.uid = uid
self.access_key = access_key
self.access_secret = access_secret
self.twitter_username = twitter_username
self.refresh_date = datetime.now()
Then I added first details to the table using the code
base_connection = BaseConnection('admin', TwitterConnection.__name__, True)
base_connection.last_connection = datetime.now()
DBSession.add(base_connection)
DBSession.flush()
tconnection = TwitterConnection(
base_connection.id,
uid=u'1234',
access_key=u'dummy',
access_secret=u'dummy',
twitter_username='dummy')
DBSession.add(tconnection)
DBSession.flush()
transaction.commit()
I'm trying to write a query to find out all the 3rd party accounts of the user with specific username
qry = BaseConnection.query.filter(
BaseConnection.username==username)
qry.all()
The above query is giving the error
`AssertionError: No such polymorphic_identity u'TwitterConnection' is defined
I am new to sqlalchemy, help is appreciated, Thank You.
Upvotes: 2
Views: 5491
Reputation: 167
Sorry,
class TwitterConnection(Base):
should be
class TwitterConnection(BaseConnection):
Upvotes: 3