sqlalchemy polymorphic_identity in list - sqlalchemy

I have the following setup and was hoping I can have polymorphic inheritance where both id_2 and id_3 discriminator values would map the same Child model. In sqlalchemy, is it possible to designate polymorphic_identity with a list of possible values?
class Parent(ScoutModel):
__tablename__ = 'parent'
identifiers = ('id_1', 'id_2', 'id_2')
id = Column(Integer, primary_key=True)
discriminator = Column(String(4), nullable=False, default=identifiers[0])
__mapper_args__ = {
'polymorphic_on': discriminator,
}
class Child(Company):
__tablename__ = 'child'
id = Column(ForeignKey('parent.id'), primary_key=True)
__mapper_args__ = {
'polymorphic_identity': '???',
}
Is there another approach that could accomplish this perhaps?
Thank you,
Fydo

Related

NotNullViolation when appending to relationship using an Association Object in SQLAlchemy?

Background
The documentation gives the following example of a parent-child-association being added by appending that association to p.children. The child is then accessed via p.children and I assume the other way is possible as well, i.e accessing the parent via c.parents.
class Association(Base):
__tablename__ = 'association'
left_id = Column(ForeignKey('left.id'), primary_key=True)
right_id = Column(ForeignKey('right.id'), primary_key=True)
extra_data = Column(String(50))
child = relationship("Child", back_populates="parents")
parent = relationship("Parent", back_populates="children")
class Parent(Base):
__tablename__ = 'left'
id = Column(Integer, primary_key=True)
children = relationship("Association", back_populates="parent")
class Child(Base):
__tablename__ = 'right'
id = Column(Integer, primary_key=True)
parents = relationship("Association", back_populates="child")
# create parent, append a child via association
p = Parent()
a = Association(extra_data="some data")
a.child = Child()
p.children.append(a)
# iterate through child objects via association, including association
# attributes
for assoc in p.children:
print(assoc.extra_data)
print(assoc.child)
Problem
I have three classes that are almost identical to those in the example:
class TopicSubcription(Base):
__tablename__ = 'topic_subscription'
topic_id = Column(ForeignKey('topic.id'), primary_key=True)
user_id = Column(ForeignKey('user.id'), primary_key=True)
subscription_date = Column('subscription_date', DateTime, nullable=True)
topic = relationship("Topic", back_populates="subscribed_users")
user = relationship("User", back_populates="followed_topics")
class Topic(Base):
__tablename__ = 'left'
id = Column(Integer, primary_key=True)
subscribed_users = relationship("Association", back_populates="topic")
class User(Base):
__tablename__ = 'right'
id = Column(Integer, primary_key=True)
followed_topics = relationship("Association", back_populates="user")
but when I run the following I get a NotNullViolation error stating that user_id violates not-null constaint.
u: User = session.query(User).filter(User.id == 'example_id1').one()
t: Topic = session.query(Topic).filter(Topic.id == 'example_id2').one()
sub = TopicSubscription(func.now())
sub.topic = t
u.followed_topics.append(sub)
session.add(u)
session.commit()
Question
Am I misinterpreting the documentation or is it wrong?
I had something quite similar just now and was going crazy trying to work it out. Abstracting to the parent/child example from the docs, what worked in my case was:
p = Parent()
a = Association(parent=p, extra_data="some data")
a.child = Child()
p.children.append(a)

SQLAlchemy conditional many to many relationship with an additional column

This is the way that I usually use for m2m relationship implementation.
(Brought from docs.sqlalchemy.org)
association_table = Table('association', Base.metadata,
Column('left_id', Integer, ForeignKey('left.id')),
Column('right_id', Integer, ForeignKey('right.id'))
)
class Parent(Base):
__tablename__ = 'left'
id = Column(Integer, primary_key=True)
children = relationship("Child",
secondary=association_table,
backref="parents")
class Child(Base):
__tablename__ = 'right'
id = Column(Integer, primary_key=True)
Is there any way for using additional columns at the association_table table?
So it should be like
association_table = Table('association', Base.metadata,
Column('left_id', Integer, ForeignKey('left.id')),
Column('right_id', Integer, ForeignKey('right.id')),
Column('is_valid', Boolean, default=True) # Add the validation column
)
class Parent(Base):
__tablename__ = 'left'
id = Column(Integer, primary_key=True)
children = relationship("Child",
secondary=association_table,
backref="parents")
# How can I do implement this??
valid_children = relationship("Child",
secondary="and_(association_table.left_id == Parent.id, association_table.right_id == Child.id)"
class Child(Base):
__tablename__ = 'right'
id = Column(Integer, primary_key=True)
I want to do query depends on is_valid column. How can I modify "secondary" attr in Parent table? Or should I fix the other part?
In this question, time_create column has the same value for all children. But in this case, I need a flag that makes able to retrieve whether this connection is still alive or not.
For example, if you implement a one-on-one chatting, there will be a chatting room consist of two-person, right?
And the table should be like as below:
association_table = Table('association', Base.metadata,
Column('left_id', Integer, ForeignKey('left.id')),
Column('right_id', Integer, ForeignKey('right.id')),
Column('is_left', Boolean, default=False) # Whether the user left or not
)
class Match(Base):
__tablename__ = 'left'
id = Column(Integer, primary_key=True)
user = relationship("User",
secondary=association_table,
backref="matches")
# How can I do implement this??
exist_user = relationship("User",
secondary="and_(association_table.left_id == Parent.id, association_table.right_id == Child.id)"
class User(Base):
__tablename__ = 'right'
id = Column(Integer, primary_key=True)
nickname = Column(String, unique=True)
How can I do for this?

How do I add more than one inheritance relationship in SQLAlchemy?

So, I have a users table, and employees table, and a tenants table.
I'm using joined table inheritance.
class User(Base):
__tablename__ = 'usr_users'
usr_user_id = Column(Integer, primary_key=True)
usr_first_name = Column(Unicode(50))
usr_last_name = Column(Unicode(50))
tenant = relationship("Tenant", uselist=False, backref="User")
usr_type = Column(String(24))
__mapper_args__ = {
'polymorphic_identity':'user',
'polymorphic_on': usr_type
}
class Tenant(User):
"""
Application's user model.
"""
__tablename__ = 'ten_tenants'
ten_tenant_id = Column(Integer, ForeignKey('usr_users.usr_user_id'), primary_key=True)
__mapper_args__ = {
'polymorphic_identity': 'tenant'
}
class Employee(User):
__tablename__ = 'emp_employees'
emp_employee_id = Column(Integer, ForeignKey('usr_users.usr_user_id'), primary_key=True)
__mapper_args__ = {
'polymorphic_identity': 'employee'
}
I've got everything working when a user becomes an employee.
user = Employee()
session.add(user)
An entry in the user table, and a value in the "type" column of "employee".
But what if I have a user that is both a employee and a tenant?
What syntax do I use to pull the user, and then add a Tenant relationship so that the resulting user has both a employee relationship and a tenant relationship?

In SQLAlchemy, what is the difference between these two models?

#models1.py
class User(Base):
__tablename__ = 'user'
id = Column(Integer, primary_key=True)
name = Column(String)
addresses = relationship("Address", backref="user")
class Address(Base):
__tablename__ = 'address'
id = Column(Integer, primary_key=True)
email = Column(String)
user_id = Column(Integer, ForeignKey('user.id'))
#models2.py
class User(Base):
__tablename__ = 'user'
id = Column(Integer, primary_key=True)
name = Column(String)
class Address(Base):
__tablename__ = 'address'
id = Column(Integer, primary_key=True)
email = Column(String)
user_id = Column(Integer, ForeignKey('user.id'))
addresses = relationship("Address", backref="user")
As you can tell, the only difference is that the "relationship" is placed in a different position. I'm confused, because in the SQLAlchemy documentation, it places it in two different places. First here, then here.
Which is the correct position of "relationship"? And is it even required? What if I leave it out...?
Both are semantically identical.
SA uses the ForeignKeys to infer the many side of one-to-many relationship.
Read Linking Relationships with Backref section which explains the bidirectional relationships. Relevant extract:
... In fact, the backref keyword is only a common shortcut for placing a
second relationship onto the Address mapping, including the
establishment of an event listener on both sides which will mirror
attribute operations in both directions. ...

Many-to-many self-referential relationship in sqlalchemy

I'm trying to make a self-referential many-to-many relationship (it means that Line can have many parent lines and many child lines) in sqlalchemy like this:
Base = declarative_base()
class Association(Base):
__tablename__ = 'association'
prev_id = Column(Integer, ForeignKey('line.id'), primary_key=True)
next_id = Column(Integer, ForeignKey('line.id'), primary_key=True)
class Line(Base):
__tablename__ = 'line'
id = Column(Integer, primary_key = True)
text = Column(Text)
condition = Column(Text)
action = Column(Text)
next_lines = relationship(Association, backref="prev_lines")
class Root(Base):
__tablename__ = 'root'
name = Column(String, primary_key = True)
start_line_id = Column(Integer, ForeignKey('line.id'))
start_line = relationship('Line')
But I get the following error:
sqlalchemy.exc.ArgumentError: Could not determine join condition between parent/
child tables on relationship Line.next_lines. Specify a 'primaryjoin' expressio
n. If 'secondary' is present, 'secondaryjoin' is needed as well.
Do you know how I could remedy this?
You should just need:
prev_lines = relationship(
Association,
backref="next_lines",
primaryjoin=id==Association.prev_id)
Since this specifies the next_lines back reference there is no need to have a next_lines relationship.
You can also do this using the remote_side parameter to a relationship: http://www.sqlalchemy.org/trac/browser/examples/adjacency_list/adjacency_list.py