SQLAlchemy: Relationship Field Values - python

I have three [MySQL] tables: Person, Role, PersonRole
class Person(Base):
__tablename__ = 'people'
id = Column(INTEGER(unsigned=True), primary_key=True)
full_name = Column(String(120), nullable=False)
email = Column(String(128))
username = Column(String(50))
last_modified = Column(TIMESTAMP, nullable=False,
server_default=text('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'))
created = Column(TIMESTAMP, server_default=text('CURRENT_TIMESTAMP'))
roles = relationship('Role',
secondary='person_role',
primaryjoin="and_(Person.id==PersonRole.person_id,"
"Role.active==True,"
"PersonRole.active==True)",
back_populates='people')
def __repr__(self):
"""String representation."""
return '''<Person(id='%s', full_name='%s')>''' % (
str(self.id), str(self.full_name)
)
class Role(Base):
__tablename__ = 'role'
id = Column(INTEGER(unsigned=True), Sequence('role_id_seq'), primary_key=True)
role_name = Column(String(16))
active = Column(Boolean, default=True)
last_modified = Column(TIMESTAMP, nullable=False,
server_default=text('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'))
created = Column(TIMESTAMP, server_default=text('CURRENT_TIMESTAMP'))
users = relationship('Person',
secondary='person_role',
primaryjoin="and_(Role.id==PersonRole.role_id,"
"Role.active==True,"
"PersonRole.active==True)",
back_populates='roles')
def __repr__(self):
"""String representation."""
return '''<Role(role_id='%s', role_name='%s')>''' % (
str(self.role_id), str(self.role_name)
)
class PersonRole(Base):
__tablename__ = 'person_role'
ds_id = Column(INTEGER(unsigned=True),
ForeignKey('person.id'), primary_key=True)
role_id = Column(INTEGER(unsigned=True),
ForeignKey('role.id'), primary_key=True)
active = Column(Boolean, default=True)
last_modified = Column(TIMESTAMP, nullable=False,
server_default=text('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'))
created = Column(TIMESTAMP,
server_default=text('CURRENT_TIMESTAMP'))
Retrieving a Person like so:
...session setup as s...
person = s.query(Person).get(123)
and to list out their roles:
for role in person.roles:
print(role.id, role.role_name, role.active)
Now, in each table there is an active column. This is to keep track of an [shocker] active status as we dont want to remove data from the table, merely keep it's state. Now to the two issues which have kept me from using SQLAlchemy altogether and has me writing and executing SQL manually -
The active portion in the final loop though displays the active state of role.active and not that of the actual relationship, person_role.active.
Removing a role will remove the relationship row instead of performing the desired action, person_role.active = 0
Even if I deactivate the relationship, adding it again will set off the Duplicate Key error.
Is there a sane, valid way to go about accomplishing this without restructuring my data?
Edit for further clarification:
The two main tables in this case are person and person_role. Person is the main table which holds our users. PersonRole holds the roles a person actually has, in the form of person.id to role.id. The Role table is merely a lookup table for the Role definition (to get the names).
I suppose what I want is a way to intercept how the ORM actually adds/removes the data. Adding should [more-or-less] do an "upsert" and removing should basically run a update query like: update person_role set active = 0 where person_id = %s and role_id = %s.
I have read a lot of the docs but tend to get lost in the terminologies. :S

Not sure I completely understood your problems, but let me try to help you:
1) This is the one I am most confused with: person.roles maps to a collection of Role entities. Ain't that what you expect?
2, 3) You have set up a relationship between Person and Role, using PersonRole as secondary. Deactivating means setting PersonRole.status to inactive, not removing it. Trying to add it again will show a Duplicate Key error indeed!
I think you want to run a query to load a PersonRole entity by ds_id and role_id, update its status to active and persist changes. I understand that sometimes this may be tedious to perform, so maybe a immediate solution that would not require you to move data around would be to map Person and Role to PersonRole. So, in spite of having Person.roles you would have Person.personRoles, so you have access to PersonRole entities and may set its status.
Of course this is a very immediate solution. SQLAlchemy is extremely featured so maybe you can intercept the Person.roles removal and customize its behaviour to set PersonRole to inactive. You may want to read more about cascading on the docs as well.
Hope I was able to clarify things a little :)

Related

SQLAlchemy one-to-many relationship - how to properly get the 'many' collection

I'm not sure I properly understand how to get the collection part of the one-to-many relationship.
class ProjectReport(db.Model):
__tablename__ = "project_reports"
id = db.Column(UUID, primary_key=True, default=uuid.uuid4)
project_id = db.Column(UUID, db.ForeignKey("projects.id"), nullable=False)
entries = db.relationship("ProducerEntry", backref="project_report", lazy="dynamic")
class ProducerEntry(Entry):
__tablename__ = "producer_entries"
__mapper_args__ = {"polymorphic_identity": "Entry"}
id = db.Column(UUID, db.ForeignKey("entries.id"), primary_key=True)
project_id = db.Column(UUID, db.ForeignKey("projects.id"), nullable=False)
project_report_id = db.Column(UUID, db.ForeignKey("project_reports.id"), nullable=True)
My problem is that I can't just access the entries field.
for entry in self.entries:
do_something(entry)
This returns NotImplementedError
I managed to get the data via hybrid property but that seems a bit of an overkill since already have the relationship, also it'd get a bit complex for further logic later on.
#hybrid_property
def entries(self):
return ProducerEntry.query.filter_by(project_report_id=self.id)
Ab additional information is that the ProjectReport is basically the common columns of the Entry and Project models, and the project_report_id is nullable, because the entries and projects are generated first and then I can generate the project reports from them. This is how I create the reports:
...
project_report = ProjectReport(date_order=entry.date_order, project_id=entry.project.id)
project_report.entries.append(entry)
...
As far as I know I don't have to add the project_report_id to the producer entry after this.
What am I missing here?
Well yeah, that relationship field returns a query, so I simply should have called:
self.entries.all()
Or anything else which is handling a query.

Working with back_populates fields in flask-admin and sqlalchemy

Consider a simple many-to-one model like this:
class Entity(Base):
__tablename__ = 'entity'
id = Column(Integer, Sequence('entity_seq'), primary_key=True)
name = Column(String(50), nullable=False)
persons = relationship('Person', back_populates='entity')
def __str__(self):
return self.name
class Person(Base):
__tablename__ = 'person'
id = Column(Integer, Sequence('person_seq'), primary_key=True)
entity_key = Column(ForeignKey('entity.id'), nullable=False)
last_name = Column(String(30), nullable=False)
first_name = Column(String(30), nullable=False)
entity = relationship('Entity', back_populates='persons')
def __str__(self):
return f'{self.first_name} {self.last_name}'
In other words, many persons belong to one entity.
If you use a flask-admin view like this:
admin.add_view(ModelView(Entity, db.session))
You might get a list like this:
Editing one of these entries can produce this output:
This presents some problems:
The persons field can be very large and take a long time to fill and probably needs to be paginated, but I can't find a way in flask-admin to cause that pagination.
Individual persons can be deleted (via the "x") but that violates the database nullable constraint on the column. It seems like flask-admin shouldn't allow that by default, or there should be a way to control it.
The persons are formatted via the __str__ attribute, but it may be necessary to format them some other way, but I can't find a way in flask-admin to do that.
What do you do in flask-admin to address these problems?

SQLalchemy built relationship between classes

I am running into a conceptual problem I do not know how to approach, which might be due my lack of knowledge with SQLalchemy. I have two classes: People and Person and I want them each to have a column to share their respective id's with each other using the relationship function.
Now, I have an endpoint in views.py which instantiates those two classes and establishes a Child / Parent relationship. Looking at the database results however, only People, the parent class has the id stored in its respective table, while the Person table in column people is None.
I know the id in person is only generated after the commit() statement and thus None for Person, and was wondering if there is a way to solve this elegantly, or do I need to first query the current people instance, retreive its id, set the id in the person table and then commit() again?
I hope my question makes sense,thank you.
'''
model.py
'''
class People(Model):
__tablename__ = 'people'
id = Column(Integer, primary_key=True)
person = relationship('Person', back_populates='people')
person_id = Column(Integer, ForeignKey('people.id'))
class Person(Model):
__tablename__ = 'people'
id = Column(Integer, primary_key=True)
people = relationship('People', uselist=False, back_populates='person')
'''
views.py
'''
#main.route('/', methods=['GET', 'POST'])
def index():
people = People()
person = Person(people_id = ?)
people.person = person
session.add(person)
session.add(people)
session.commit()
I regret that I have not yet understood your question. However, since your code contains some errors, I will first write you my corrected variant.
class People(Model):
__tablename__ = 'people'
id = Column(Integer, primary_key=True)
person = relationship('Person', back_populates='people')
person_id = Column(Integer, ForeignKey('person.id'))
class Person(Model):
__tablename__ = 'person'
id = Column(Integer, primary_key=True)
people = relationship('People', back_populates='person')
def index():
person = Person()
people = People()
people.person = person
session.add(person)
session.add(people)
session.commit()
The question of gittert seems justified to me. It makes no sense to save the ForeignKey in both tables on the referenced identifiers of the other model.
What do you want to achieve?
If you're looking for an actual column in your database for your 'relationships', you won't find them. Your .people and .person are virtual relationships created in Python without any interaction with the SQL database.

Flask SQL-Alchemy | MySql - Multiple Foreign Keys issues

class Role(db.Model):
__tablename__ = 'roles'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(64), unique=True)
default = db.Column(db.Boolean, default=False, index=True)
permissions = db.Column(db.Integer)
class Devices(db.Model):
__tablename__ = 'devices'
id = db.Column(db.Integer, primary_key=True)
purpose = db.Column('purpose', db.String(64))
type = db.Column('type', db.String(64))
name = db.Column('name', db.String(64))
channel = db.Column('channel', db.Integer)
role_id = db.Column('role_id', db.Integer)
role_permissions = db.Column('role_permissions', db.Integer)
role = db.ForeignKeyConstraint(['role_id', 'role_permissions'], ['roles.id', 'roles.permissions'])
Then I would expect this to work:
dev = Devices(purpose="lights",type="tcp",name="zeus",channel=8)
role = Role.query.first()
dev.role = role
db.session.add(dev)
db.session.commit()
But once persisted, role_id and role_permissions get null value. Why? Whats the right way to do this??
You need to define a relationship in addition to the foreign key.
A foreign key is just a database-level constraint to ensure you cannot reference rows that don't exist (additionally, it helps SQLAlchemy setup a relationship without you specifying another time how the two tables are linked).
You want this in your model:
class Devices(db.Model):
__table_args__ = (db.ForeignKeyConstraint(['role_id', 'role_permissions'], ['roles.id', 'roles.permissions']),)
# ...
role = db.relationship('Role', backref=db.backref('devices'))
By doing so, device.role = some_role will properly populate the foreign keys, and in addition each Role instance will have a devices collection that gives you access to its associated devices.
The SQLAlchemy tutorial also has a section about relationships:
http://docs.sqlalchemy.org/en/rel_1_0/orm/tutorial.html#building-a-relationship
You can pretty much follow it; Flask-SQLAlchemy and plain SQLalchemy don't really differ - Flask-SQLAlchemy simply makes many things accessible via the db object to avoid importing them explicitly.
By the way, since Role.id is the primary key, you don't need to include role_permissions in the foreign key - you cannot have more than one role the same ID since the primary key is always unique. This makes your model even easier:
class Devices(db.Model):
# ...
role_id = db.Column('role_id', db.Integer, db.ForeignKey('roles.id'))
role = db.relationship('Role', backref=db.backref('devices'))
You can also get rid of the role_permissions column in your Devices model (which, by the way, should be named Device). If you need the permissions, simply get it from the role (if you usually need it, add lazy=False to the foreign key, then querying a device will always join the role table to avoid extra queries)

Query items from granchildren objects

I have a Flask python application that has a set of related tables that are chained together through foreign keys. I would like to be able to return an aggregate list of records from one table that are related to a distant table. However, I am struggling to understand how sqlalchemy does this through object relationships.
For example, there are three objects I'd like to join (challenge and badge) with two tables (talent_challenge and badge) to be able to query for all badges related to a specific challenge. In SQL, this would look something like:
SELECT b.id, b.name
FROM badge b
INNER JOIN talent_challenge tc ON tc.talent_id = b.talent_id
WHERE tc.challenge_id = 21
The 'talent' and 'challenge' tables are not needed in this case, since I only need the talent and challenge IDs (in 'talent_challenge') for the relationship. All of the interesting detail is in the badge table.
I am able to use sqlalchemy to access the related talent from a challenge using:
talents = db.relationship('TalentModel', secondary='talent_challenge')
And I can then reference talent.badges for each of those talents to get the relevant badges related to my initial challenge. However, there can be redundancy, and this list of badges isn't contained in a single object.
A stripped-down version of the three models are:
class TalentModel(db.Model):
__tablename__ = 'talent'
# Identity
id = db.Column(db.Integer, primary_key=True)
# Relationships
challenges = db.relationship('ChallengeModel', secondary='talent_challenge',)
# badges (derived as backref from BadgeModel)
class ChallengeModel(db.Model):
__tablename__ = 'challenge'
# Identity
id = db.Column(db.Integer, primary_key=True)
member_id = db.Column(db.Integer, db.ForeignKey('member.id'))
# Relationships
talents = db.relationship('TalentModel', secondary='talent_challenge', order_by='desc(TalentModel.created_at)')
class BadgeModel(db.Model):
__tablename__ = 'badge'
# Identity
id = db.Column(db.Integer, primary_key=True)
talent_id = db.Column(db.Integer, db.ForeignKey('talent.id'))
# Parents
talent = db.relationship('TalentModel', foreign_keys=[talent_id], backref="badges")
I also have a model for the associative table, 'talent_challenge':
class TalentChallengeModel(db.Model):
__tablename__ = 'talent_challenge'
# Identity
id = db.Column(db.Integer, primary_key=True)
talent_id = db.Column(db.Integer, db.ForeignKey('talent.id'))
challenge_id = db.Column(db.Integer, db.ForeignKey('challenge.id'))
# Parents
talent = db.relationship('TalentModel', uselist=False, foreign_keys=[talent_id])
challenge = db.relationship('ChallengeModel', uselist=False, foreign_keys=[challenge_id])
I would like to better understand sqlalchemy (or specifically, flask-sqlalchemy) to allow me to construct this list of badges from the challenge object. Is db.session.query of BadgeModel my only option?
UPDATED 1/23/2015:
My blocker on my project was solved by using the following:
#property
def badges(self):
from app.models.sift import BadgeModel
from app.models.relationships.talent import TalentChallengeModel
the_badges = BadgeModel.query\
.join(TalentChallengeModel, TalentChallengeModel.talent_id==BadgeModel.talent_id)\
.filter(TalentChallengeModel.challenge_id==self.id)\
.all()
return the_badges
Wrapping the query in a function got around the issues I was having with the name BadgeModel not being defined and not being able to be imported in the model otherwise. The #property decorator allows me to just reference this as challenge.badges later in the view.
However, I am still interested in understanding how to do this as a relationship. Some searching elsewhere led me to believe this would work:
badges = db.relationship('BadgeModel',
secondary="join(BadgeModel, TalentChallengeModel, BadgeModel.talent_id == TalentChallengeModel.talent_id)",
secondaryjoin="remote([id]) == foreign(TalentChallengeModel.challenge_id)",
primaryjoin="BadgeModel.talent_id == foreign(TalentChallengeModel.talent_id)",
viewonly=True,
)
Because of other unresolved issues in my application environment, I can't fully test this (e.g., adding this code breaks Flask-User in my site) but would like to know if this is correct syntax and if there is any disadvantage to this over the query-in-function solution.

Categories

Resources