I'm trying to implement a user-facing PreviewList of Articles, which will keep its size even if an Article is deleted. So if the list has four objects [1, 2, 3, 4] and one is deleted, I want it to contain [1, 2, None, 4].
I'm using a relationship with a secondary table. Currently, deleting either Article or PreviewList will delete the row in that table. I've experimented with cascade options, but they seem to affect the related items directly, not the contents of the secondary table.
The snippet below tests for the desired behaviour: deleting an Article should preserve the row in ArticlePreviewListAssociation, but deleting a PreviewList should delete it (and not the Article).
In the code below, deleting the Article will preserve the ArticlePreviewListAssociation, but pl.articles does not treat that as a list entry.
from db import DbSession, Base, init_db
from sqlalchemy import Column, String, Integer, ForeignKey
from sqlalchemy.orm import relationship
session = DbSession()
class Article(Base):
__tablename__ = 'articles'
id = Column(Integer, primary_key=True)
title = Column(String)
class PreviewList(Base):
__tablename__ = 'preview_lists'
id = Column(Integer, primary_key=True)
articles = relationship('Article', secondary='associations')
class ArticlePreviewListAssociation(Base):
__tablename__ = 'associations'
article_id = Column(Integer, ForeignKey('articles.id'), nullable=True)
previewlist_id = Column(Integer, ForeignKey('preview_lists.id'), primary_key=True)
article = relationship('Article')
preview_list = relationship('PreviewList')
init_db()
print(f"Creating test data")
a = Article(title="StackOverflow: 'Foo' not setting 'Bar'?")
pl = PreviewList(articles=[a])
session.add(a)
session.add(pl)
session.commit()
print(f"ArticlePreviewListAssociations: {session.query(ArticlePreviewListAssociation).all()}")
print(f"Deleting PreviewList")
session.delete(pl)
associations = session.query(ArticlePreviewListAssociation).all()
print(f"ArticlePreviewListAssociations: should be empty: {associations}")
if len(associations) > 0:
print("FAIL")
print("Reverting transaction")
session.rollback()
print("Deleting article")
session.delete(a)
articles_in_list = pl.articles
associations = session.query(ArticlePreviewListAssociation).all()
print(f"ArticlePreviewListAssociations: should not be empty: {associations}")
if len(associations) == 0:
print("FAIL")
print(f"Articles in PreviewList: should not be empty: {articles_in_list}")
if len(articles_in_list) == 0:
print("FAIL")
# desired outcome: pl.articles should be [None], not []
print("Reverting transaction")
session.rollback()
This may come down to "How can you make a many-to-many relationship where pk_A == 1 and pk_B == NULL include the None in A's list?"
The given examples would seem to assume that the order of related articles is preserved, even upon deletion. There are multiple approaches to that, for example the Ordering List extension, but it is easier to first solve the problem of preserving associations to deleted articles. This seems like a use case for an association object and proxy.
The Article class gets a new relationship so that deletions cascade in a session. The default ORM-level cascading behavior is to set the foreign key to NULL, but if the related association object is not loaded, we want to let the DB do it, so passive_deletes=True is used:
class Article(Base):
__tablename__ = 'articles'
id = Column(Integer, primary_key=True)
title = Column(String)
previewlist_associations = relationship(
'ArticlePreviewListAssociation', back_populates='article',
passive_deletes=True)
Instead of a many to many relationship PreviewList uses the association object pattern, along with an association proxy that replaces the many to many relationship. This time the cascades are a bit different, since the association object should be deleted, if the parent PreviewList is deleted:
class PreviewList(Base):
__tablename__ = 'preview_lists'
id = Column(Integer, primary_key=True)
article_associations = relationship(
'ArticlePreviewListAssociation', back_populates='preview_list',
cascade='all, delete-orphan', passive_deletes=True)
articles = association_proxy(
'article_associations', 'article',
creator=lambda a: ArticlePreviewListAssociation(article=a))
Originally the association object used previewlist_id as the primary key, but then a PreviewList could contain a single Article only. A surrogate key solves that. The foreign key configurations include the DB level cascades. These are the reason for using passive deletes:
class ArticlePreviewListAssociation(Base):
__tablename__ = 'associations'
id = Column(Integer, primary_key=True)
article_id = Column(
Integer, ForeignKey('articles.id', ondelete='SET NULL'))
previewlist_id = Column(
Integer, ForeignKey('preview_lists.id', ondelete='CASCADE'),
nullable=False)
# Using a unique constraint on a nullable column is a bit ugly, but
# at least this prevents inserting an Article multiple times to a
# PreviewList.
__table_args__ = (UniqueConstraint(article_id, previewlist_id), )
article = relationship(
'Article', back_populates='previewlist_associations')
preview_list = relationship(
'PreviewList', back_populates='article_associations')
With these changes in place no "FAIL" is printed.
Related
I have a many to many association using association proxies as follows:
import sqlalchemy.orm as orm
import sqlalchemy as sa
import sqlalchemy.ext.associationproxy as ap
class Asset(BaseModel):
__tablename__ = 'assets'
id = sa.Column(sa.Integer, primary_key=True)
name = sa.Column(sa.VARCHAR(255))
asset_tags = orm.relationship(
"AssetTag", back_populates='asset', cascade='all, delete-orphan')
tags = ap.association_proxy(
"asset_tags", "tag",
creator=lambda tag: AssetTag(tag=tag))
class AssetTag(BaseModel):
__tablename__ = 'asset_tags'
asset_id = sa.Column(sa.Integaer, sa.ForeignKey("assets.id"), primary_key=True)
tag_id = sa.Column(sa.Integer, alsach.ForeignKey("tags.id"), primary_key=True)
asset = orm.relationship("Asset", back_populates='asset_tags')
tag = orm.relationship("Tag", back_populates='asset_tags')
class Tag(BaseModel):
__tablename__ = 'tags'
id = sa.Column(sa.Integer, primary_key=True)
name = sa.Column(sa.VARCHAR(255))
asset_tags = orm.relationship("AssetTag",
back_populates='tag',
cascade='all, delete-orphan')
assets = ap.association_proxy("asset_tags", "asset",
creator=lambda asset: AssetTag(asset=asset))
Note that the asset_tas table has a uniqueness constraint on (asset_id, tag_id).
If I do
with Session() as s:
a = s.get(Asset, 1)
tag = a.tags[0]
a.tags.append(tag)
s.commit()
SQLAlchemy creates a new AssetTag between Asset<1> and Tag<31> (example number), and tries to commit that, violating the uniqueness constraint.
pymysql.err.IntegrityError: (1062, "Duplicate entry '1-31' for key 'PRIMARY'")
Is there any way to make asset.tags have the behavior of a set, where adding an existing item is skipped?
asset.tags.append(tag)
asset.tags.append(tag) # fails silently
You can pass set as the relationship's collection_class argument. This will have the collection behave as a set rather than as a list.
asset_tags = orm.relationship(
"AssetTag",
back_populates='asset',
cascade='all, delete-orphan',
collection_class=set,
)
Note that you must use set methods when operating on the collection, for example
asset.tags.add(some_tag)
not
asset.tags.append(some_tag)
Follwing the documentation, I have the following tables defined and populated but no relations have been defined yet.
class CountryAssociation(Base):
__tablename__ = 'Country_Region_Mapping'
country_id = Column(Integer, ForeignKey('countries.uid'), primary_key=True)
region_id = Column(Integer, ForeignKey('regions.uid'), primary_key=True)
region = relationship('Region', back_populates='country')
country = relationship('Countries', back_populates='region')
extra_data = Column(String(50))
class Countries(Base):
__tablename__ = 'countries'
uid = Column(Integer, primary_key=True)
countryname = Column('English_short_name',
String(255), unique=True, nullable=False)
region = relationship('CountryAssociation',
back_populates='country')
class Region(Base):
__tablename__ = 'regions'
uid = Column(Integer, primary_key=True)
region = Column(String(255), unique=True, nullable=False)
country = relationship('CountryAssociation',
back_populates='region')
I now want to create many to many relations between the tables. docs
Base = automap_base() #reflecting the orm way
engine = create_engine('sqlite:///mydatabse.db')
Base.prepare(engine, reflect=True)
Session = sessionmaker(bind=engine)
session = Session()
table_countries = Base.classes.countries
table_regions = Base.classes.regions
r = session.query(table_regions).filter(table_regions.region == "Western Europe").first()
c = session.query(table_countries).filter(table_countries.English_short_name == "Germany").first()
c.region.append(r) # this fails with
AttributeError: 'countries' object has no attribute 'region'
This however works:
c.countryname # Germany
I dont get what I am doing wrong here (beginner)...
Because you've used the association object pattern with extra_data the automap relationship detection will not recognize Country_Region_Mapping as a secondary table of a many to many:
If the table contains two and exactly two ForeignKeyConstraint objects, and all columns within this table are members of these two ForeignKeyConstraint objects, the table is assumed to be a “secondary” table, and will not be mapped directly.
Put another way: not all columns within Country_Region_Mapping are members of the foreign key constraints, so the table is not a secondary, so no many to many relationship is created.
Another thing you've overlooked is the naming scheme. If you had a working secondary table, then the created relationship would be named regions_collection by default (because of the plural table name).
What does happen is that the many to one / one to many relationships between regions and Country_Region_Mapping, and countries and Country_Region_Mapping are created:
In [22]: table_countries.country_region_mapping_collection
Out[22]: <sqlalchemy.orm.attributes.InstrumentedAttribute at 0x7f2d600fb258>
In [23]: table_regions.country_region_mapping_collection
Out[23]: <sqlalchemy.orm.attributes.InstrumentedAttribute at 0x7f2d600fbaf0>
In [28]: table_country_region_mapping.countries
Out[28]: <sqlalchemy.orm.attributes.InstrumentedAttribute at 0x7f2d535a2990>
In [29]: table_country_region_mapping.regions
Out[29]: <sqlalchemy.orm.attributes.InstrumentedAttribute at 0x7f2d535a2938>
Note that due to plural table naming the scalar relationship attributes on Country_Region_Mapping also have plural naming.
With these in mind you'd add a new association like so:
In [36]: c.country_region_mapping_collection.append(
...: Base.classes.Country_Region_Mapping(countries=c, regions=r))
Let's assume we have two tables in a many to many relationship as shown below:
class User(db.Model):
__tablename__ = 'user'
uid = db.Column(db.String(80), primary_key=True)
languages = db.relationship('Language', lazy='dynamic',
secondary='user_language')
class UserLanguage(db.Model):
__tablename__ = 'user_language'
__tableargs__ = (db.UniqueConstraint('uid', 'lid', name='user_language_ff'),)
id = db.Column(db.Integer, primary_key=True)
uid = db.Column(db.String(80), db.ForeignKey('user.uid'))
lid = db.Column(db.String(80), db.ForeignKey('language.lid'))
class Language(db.Model):
lid = db.Column(db.String(80), primary_key=True)
language_name = db.Column(db.String(30))
Now in the python shell:
In [4]: user = User.query.all()[0]
In [11]: user.languages = [Language('1', 'English')]
In [12]: db.session.commit()
In [13]: user2 = User.query.all()[1]
In [14]: user2.languages = [Language('1', 'English')]
In [15]: db.session.commit()
IntegrityError: (IntegrityError) column lid is not unique u'INSERT INTO language (lid, language_name) VALUES (?, ?)' ('1', 'English')
How can I let the relationship know that it should ignore duplicates and not break the unique constraint for the Language table? Of course, I could insert each language separately and check if the entry already exists in the table beforehand, but then much of the benefit offered by sqlalchemy relationships is gone.
The SQLAlchemy wiki has a collection of examples, one of which is how you might check uniqueness of instances.
The examples are a bit convoluted though. Basically, create a classmethod get_unique as an alternate constructor, which will first check a session cache, then try a query for existing instances, then finally create a new instance. Then call Language.get_unique(id, name) instead of Language(id, name).
I've written a more detailed answer in response to OP's bounty on another question.
I would suggest to read Association Proxy: Simplifying Association Objects. In this case your code would translate into something like below:
# NEW: need this function to auto-generate the PK for newly created Language
# here using uuid, but could be any generator
def _newid():
import uuid
return str(uuid.uuid4())
def _language_find_or_create(language_name):
language = Language.query.filter_by(language_name=language_name).first()
return language or Language(language_name=language_name)
class User(Base):
__tablename__ = 'user'
uid = Column(String(80), primary_key=True)
languages = relationship('Language', lazy='dynamic',
secondary='user_language')
# proxy the 'language_name' attribute from the 'languages' relationship
langs = association_proxy('languages', 'language_name',
creator=_language_find_or_create,
)
class UserLanguage(Base):
__tablename__ = 'user_language'
__tableargs__ = (UniqueConstraint('uid', 'lid', name='user_language_ff'),)
id = Column(Integer, primary_key=True)
uid = Column(String(80), ForeignKey('user.uid'))
lid = Column(String(80), ForeignKey('language.lid'))
class Language(Base):
__tablename__ = 'language'
# NEW: added a *default* here; replace with your implementation
lid = Column(String(80), primary_key=True, default=_newid)
language_name = Column(String(30))
# test code
user = User(uid="user-1")
# NEW: add languages using association_proxy property
user.langs.append("English")
user.langs.append("Spanish")
session.add(user)
session.commit()
user2 = User(uid="user-2")
user2.langs.append("English") # this will not create a new Language row...
user2.langs.append("German")
session.add(user2)
session.commit()
I'm using SQLAlchemy to represent a relationship between authors. I'd like to have authors related to other authors (coauthorshp), with extra data in the relation, such that with an author a I can find their coauthors.
How this is done between two different objects is this:
class Association(Base):
__tablename__ = 'association'
left_id = Column(Integer, ForeignKey('left.id'), primary_key=True)
right_id = Column(Integer, ForeignKey('right.id'), primary_key=True)
extra_data = Column(String(80))
child = relationship('Child', backref='parent_assocs')
class Parent(Base):
__tablename__ = 'left'
id = Column(Integer, primary_key=True)
children = relationship('Association', backref='parent')
class Child(Base):
__tablename__ = 'right'
id = Column(Integer, primary_key=True)
but how would I do this in my case?
The nature of a coauthorship is that it is bidirectional. So, when you insert the tuple (id_left, id_right) into the coauthorship table through a coauthoship object, is there a way to also insert the reverse relation easily? I'm asking because I want to use association proxies.
if you'd like to literally have pairs of rows in association, that is, for every id_left, id_right that's inserted, you also insert an id_right, id_left, you'd use an attribute event to listen for append events on either side, and produce an append in the other direction.
If you just want to be able to navigate between Parent/Child in either direction, just a single row of id_left, id_right is sufficient. The examples in the docs regarding this kind of mapping illustrate the whole thing.
This is my Flask-SQLAlchemy Declarative code:
from sqlalchemy.ext.associationproxy import association_proxy
from my_flask_project import db
tagging = db.Table('tagging',
db.Column('tag_id', db.Integer, db.ForeignKey('tag.id', ondelete='cascade'),
primary_key=True),
db.Column('role_id', db.Integer, db.ForeignKey('role.id', ondelete='cascade'),
primary_key=True)
)
class Tag(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(100), unique=True, nullable=False)
def __init__(self, name=None):
self.name = name
class Role(db.Model):
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('user.id', ondelete='cascade'))
user = db.relationship('User', backref=db.backref('roles', cascade='all',
lazy='dynamic'))
...
tags = db.relationship('Tag', secondary=tagging, cascade='all',
backref=db.backref('roles', cascade='all'))
tag_names = association_proxy('tags', 'name')
__table_args__ = (
db.UniqueConstraint('user_id', 'check_id'),
)
I think it's pretty standard many-to-many tagging solution. Now, I'd like to get all tags for a role and set new set of tags to a role.
The first one is pretty easy:
print role.tags
print role.tag_names
However, the second one made me stumbling upon my Python code all day long :-( I thought I could do this:
role.tag_names[:] = ['red', 'blue', 'white']
...or at least something similar using role.tags[:] = ..., but everything I invented raised many integrity errors, as SQLAlchemy didn't check if there are any existing tags and tried to insert all of them as completely new entities.
My final solution is:
# cleanup input
tag_names = set(filter(None, tag_names))
# existings tags to be updated
to_update = [t for t in role.tags if t.name in tag_names]
# existing tags to be added
to_add = list(
Tag.query.filter(Tag.name.in_(tag_names - set(role.tag_names)))
)
# tags to be created
existing_tags = to_update + to_add
to_create = [Tag(name) for name in tag_names - set([t.name for t in existing_tags])]
# assign new tags
role.tags[:] = existing_tags + to_create
# omitted bonus: find a way how to get rid of orphan tags
The question is: Is this really the right solution? Is there any more elegant way how to solve this trivial problem? I thik the whole matter is related to this question. Maybe I'm just silly, maybe I'm making things overcomplicated... anyway, thank you for any suggestions!
Actually SQLAlchemy does check if the object exists by calling Session.merge(). But it does it by identity — its primary key. The simplest solution is to make name primary key and everything will work. Sure, the three tables chain will become redundant in this case unless you are going to add some additional fields into Tag (e.g. counter).