SQLAlchemy Adjacency List Relationships cascading not updating on parents - python-3.x

From the SQLAlchemy documentation I've been able to create the parent-child relationship that I need.
class Node(db.Model):
__tablename__ = 'node'
id = db.Column(db.Integer, primary_key=True)
parent_data = db.Column(db.Integer, db.ForeignKey('node.data'))
data = db.Column(db.String(50))
children = db.relationship("Node", remote_side=[data])
and then I create my objects and everything works fine
node1 = Node(data='Node1')
node2 = Node(data='Node2', children=node1)
db.session.add(node2)
db.session.commit()
But if I change the data in node1 (which is node2 child), node2.parent_data won't update accordingly.
Tried using backref for children, cascading the relationship but without success.
I can create many to many relationships with the same class using association tables, but would be very nice to get this working.

Related

How to get a one-to-one relationship?

There are for example 2 tables: Parent , Child
I'm trying to build a one-to-one relationship.
class Parent(Base):
__tablename__ = "parent"
id = Column(Integer, primary_key=True)
class Child(Base):
__tablename__ = "child"
id = Column(Integer, primary_key=True)
parent_id = Column(Integer, ForeignKey("parent.id"))
parent = relationship("Parent", backref=backref("child", uselist=False))
I'm trying to build a one-to-one relationship.
parent_one = Parent()
db.add(parent_one)
db.commit()
child = Child(parent=parent_one)
db.add(child)
db.commit()
The code above will work and in the child table I will see: enter image description here
But if I execute the following code, then I won't get any exceptions (even though Parent 1 already has Child 1):
parent_one = db.query(Parent).filter(Parent.id == 1).first()
child = Child(parent=parent_one)
db.add(child)
db.commit()
And in the child table I will see: - enter image description here
I was expecting an exception and this confuses me even more... Why is this happening?

Multiple foreign keys referencing same table column in SQLAlchemy

I am creating a rating system. Rating is a table which contains individual ratings as rows. Each rating has a "rater" and a "ratee". These two columns reference a different table, "User", by means of a foreign key. However, they both reference the same user.id column. Code:
class Rating(db.Model):
id = db.Column(db.Integer, primary_key=True)
rater_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
ratee_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
And here is how they are represented from within the User class (table):
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(32), unique=True, nullable=False)
ratesOfOthers = db.relationship('Rating', backref='rater', lazy=True)
ratingsByOthers = db.relationship('Rating', backref='ratee', lazy=True)
Now, when I try to use this relationship, I am presented with the following error:
sqlalchemy.exc.AmbiguousForeignKeysError: Could not determine join condition between parent/child tables on relationship User.ratesOfOthers - there are multiple foreign key paths linking the tables. Specify the 'foreign_keys' argument, providing a list of those columns which should be counted as containing a foreign key reference to the parent table.
I have tried using the foreign_keys argument from inside the User class, but that did nothing. Any help would be appreciated.
The assumption is that in your scenario a user can rate other users, and they (themselves) can be rated. Basically, there is one table called User referencing other users within an application.
A relationship in which instances of a class are linked to other instances of the same class is called self-referential relationship, and that is exactly what you have here.
Here is a diagram that represents this self-referential many-to-many relationship that keeps track of ratings:
The Ratings table is the association table of the relationship. The foreign keys in this table are pointing at entries in the User table since it is linking users to users.
To add this table to your database, this is how you can go about it:
ratings = db.Table('ratings'
db.Column('my_ratings_id', db.Integer, db.ForeignKey('user.id'))
db.Column('other_people_rating_id', db.Integet, db.ForeignKey('user.id'))
)
This is an auxiliary table (directly-translated as seen above) that has no data other than foreign keys. It is, therefore, created without an associated model class.
To declare the many-to-many relationship in the User table, add this:
class User(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(32), unique=True, nullable=False)
def __repr__(self):
return f'{self.username}'
my_ratings = db.relationship(
'User',
secondary=ratings,
primaryjoin=(ratings.c.my_ratings_id == id),
secondaryjoin=(ratings.c.other_people_rating_id == id),
backref = db.backref('other_people_rating', lazy='dynamic'), lazy='dynamic'
)
I am defining the relationship as seen from the left side user with the name my_ratings because when you query this relationship from the left side, you will get a list of all those on the right side. Visually, this is what I mean:
Examining all the arguments of the db.relationship() call, you will see that:
User is the right side entity of the relationship.
secondary configures the ratings association table
primaryjoin indicates the condition that links the left side entity with the association table. The user id should match my_ratings_id
secondaryjoin indicates the condition that links the right side entity with the association table. Again, other_people_rating_id should match the user id
backref defines how this relationship will be accessed from the right side entity. From the left side, the relationship is named my_ratings, so from the right side, I decided to name it other_people_rating to represent all the left side users that are linked to the target user in the right side.
The dynamic mode is used to set up the query to not run until specifically requested.
The second lazy parameter applies to the left side query instead of the right side.

Deletion of a row from an association table

I am working on an app using python3 and SqlAlchemy for SQLite3 database management. I have some tables that have a Many to Many relationship. I've created an association table to handle this relationship.
Class Machine(Base):
__tablename__ 'machine'
machine_ID = Column(Integer, primary_key=True)
etc...
Class Options(Base):
__tableName__ 'options'
options_ID = Column(Integer, primary_key=True)
etc...
The association table
Machine_Options = table('machine_options', Base.metadata,
Column('machine_FK', Integer, ForeignKey('machine.machine_ID'),
primary_key=True),
Column('options_FK',Integer, ForeignKey('options.options_ID'),
primary_key=True))
All the items for the Machine and Options are inserted independently. When I want to associate a machine with an option I use an append query which works very well.
My problem is when I want to break this association between a machine and an option. I have tried a direct row deletion from the association table using a FILTER() clause on the machine_FK and the options_FK but SqlAlchemy gives me an error informing me that 'Machine_Options' table has no field 'machine_FK'.
I have tried to remove the row from 'Machine_Options' indirectly using joins with the machine and options table but received another error that I can not delete or update using joins.
I am looking for the code to only delete a row from the association table without affecting the original machine or options table.
So far my internet search has been fruitless.
The answer to my problem is to use myparent.children.remove(somechild)
The association is made using machine.children.append(option)
Using the same code as the 'append' and substituting 'remove' unmakes the association
The code:
def removeOption(machineKey, OptionKey):
session = connectToDatabase()
machineData = session.query(Machine).filter(Machine.machine_ID == machineKey).one()
optionData = session.query(Options).filter(Options. options_ID == OptionKey).one()
machineData.children.remove(optionData)
session.add(machineData)
session.commit()
session.close()

Many to Many relationship Flask sqlalchemy and marshmallow

Suppose i have 2 tables , Table 1 consist of users info and Table 2 consist of Branch info.
table 1 and table 2 is related to each other by many to many relationship.
e.g 1 user can work in multiple branches and 1 branch can have multiple users.
so here there's no parent child concept. i was wondering if i have to create another table with schema and relate it to user and branch table using foreign key or shall i create an association table.
I have done this :
class UserBranchMap(db.Model):
id = db.Column(db.Integer, primary_key=True)
branch_id = db.Column(db.Integer,db.ForeignKey('branch.id'))
branch = db.relationship("Branch", backref=db.backref("UBMbranch", lazy="dynamic"))
user_id = db.Column(db.Integer,db.ForeignKey('user.id'))
user = db.relationship("User", backref=db.backref("UBMuser", lazy="dynamic"))
created_at = db.Column(db.VARCHAR(20), nullable = False)
created_by = db.Column(db.VARCHAR(20), nullable = False)
class UserBranchMapSchema(ma.Schema):
branch = fields.Nested(branch_schema)
user = fields.Nested(user_schema)
class Meta:
fields = ('branch_id','user_id','created_at','created_by')
userbranchmap_schema = UserBranchMapSchema()
userbranchmaps_schema = UserBranchMapSchema(many = True)
what's the difference between association table and this mapping table ?
If I understand you correctly, you're asking about the difference between an association table
UserBranches = db.Table(
'user_branches',
db.Column('user_id', db.Integer, db.ForeignKey('user.id'), primary_key=True),
db.Column('branch_id', db.Integer, db.ForeignKey('branch.id'), primary_key=True)
)
and a mapping table
class UserBranch(db.Model):
id = db.Column(db.Integer, primary_key=True)
user = db.relationship("User", backref=db.backref("UBMuser", lazy="dynamic"))
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
branch = db.relationship("Branch", backref=db.backref("UBMbranch", lazy="dynamic"))
branch_id = db.Column(db.Integer, db.ForeignKey('branch.id'))
On a database level, there is no real difference between them. If you only want to have a strict Many To Many relationship between two tables, just use an association table. Because of SQLAlchemy, you never have to do anything with it, because joining through this table happens automatically, as soon as you join Users to Branches or the reverse.
If, however, you want to do more, like have it actually denote a relationship, then the mapping table like you wrote it the way to go, because it behaves exactly like a normal table. This means you can use it like UserBranch.created_at or even query it directly if you want.

Change SQLAlchemy __tablename__

I am using SQLAlchemy to handle requests from an API endpoint; my database tables (I have hundreds) are differentiated via a unique string (e.g. test_table_123)...
In the code below, __tablename__ is static. If possible, I would like that to change based on the specific table I would like to retrieve, as it would be tedious to write several hundred unique classes.
from config import db, ma # SQLAlchemy is init'd and tied to Flask in this config module
class specific_table(db.Model):
__tablename__ = 'test_table_123'
var1 = db.Column(db.Integer, primary_key=True)
var2 = db.Column(db.String, index=True)
var3 = db.Column(db.String)
class whole_table_schema(ma.ModelSchema):
class Meta:
model = specific_table
sqla_session = db.session
def single_table(table_name):
# collect the data from the unique table
my_data = specific_table().query.order_by(specific_table.level_0).all()
Thank you very much for your time in advance.
You can use reflect feature of SQLAlchemy.
engine = db.engine
metadata = MetaData()
metadata.reflect(bind=engine)
and finally
db.session.query(metadata.tables[table_name])
If you want smoother experience with querying, as previous solution cannot offer one, you might declare and map your tables: tables = {table_name: create_table(table_name) for table_name in table_names}, where create_table constructs models with different __tablename__. Instead of creating all tables at once, you can create them on demand.

Resources