Custom mapping hybrid_property - sqlalchemy

I have the problems with the configuration of the mapper models with hybrid_property.
How to properly configure hybrid_property?
class FooTable:
__tablename__ = 'foo'
name_translations = Column(JSONB, nullable=False)
name = hybrid_property(...)
class Foo:
def __init__(self, name_translations):
self.name_translations = name_translations
# columns = [name_translations]
attributes = tuple(columns) + FooTable.__table_args__
table = Table(FooTable.__tablename__, metadata, *attributes)
mapper(Foo, table, properties=**HOW???**)
Thanks for help.

Related

SQLAlchemy - Pytest - One or more mappers failed to initialize - can't proceed with initialization of other mappers

I am trying to create a DB with user and project tables having a many-to-many relation. A user can be a part of many projects and a project can have many users. I created the models for the user, project and the association tables and am able to create the database using alembic without any issues.
However, when i try to use the models. it throws an exception
sqlalchemy.exc.InvalidRequestError: One or more mappers failed to initialize - can't proceed with initialization of other mappers. Triggering mapper: 'mapped class User->users'. Original exception was: reverse_property 'user' on relationship User.projects references relationship ProjectUsers.user, which does not reference mapper mapped class User->users
Here is the code for the models
from app.modules.database import BaseModel
from sqlalchemy import Column, Integer, String, ForeignKey, JSON
from sqlalchemy.orm import relationship
from sqlalchemy_json import mutable_json_type
class Project(BaseModel):
__tablename__ = "projects"
__table_args__ = {'extend_existing': True}
project_name = Column(String, unique=True)
project_desc = Column(String)
users = relationship('app.modules.projects.project_models.ProjectUsers', back_populates='project')
owner_id = Column(Integer, ForeignKey('users.id'))
class ProjectUsers(BaseModel):
__tablename__ = "project_users"
__table_args__ = {'extend_existing': True}
user_id = Column(Integer, ForeignKey('users.id'), primary_key=True)
project_id = Column(Integer, ForeignKey('projects.id'), primary_key=True)
user = relationship("app.modules.users.user_models.User", back_populates="projects")
project = relationship("app.modules.projects.project_models.Project", back_populates="users")
The User model is :
from app.modules.database import BaseModel
from sqlalchemy import Column, Integer, String, ForeignKey
from sqlalchemy.orm import relationship
from dataclasses import dataclass
#dataclass
class User(BaseModel):
__tablename__ = "users"
__table_args__ = {'extend_existing': True}
email = Column(String, unique=True)
username = Column(String, unique=True)
f_name = Column(String)
l_name = Column(String)
m_name = Column(String)
role_id = Column(Integer, ForeignKey('roles.id'))
#user_role = relationship('app.modules.roles.role_models.Role', backref="users")
# A user can have access to many projects where they are not the owners of the project.
#projects = relationship('app.modules.projects.project_models.Project', secondary="project_users", back_populates="users")
projects = relationship('app.modules.projects.project_models.ProjectUsers', back_populates='user')
def __repr__(self):
return '<User: {}>'.format(self.username)
Alembic creates the corresponding tables with the proper associations without any issues, but running a simple test like creating a new User() or Project() throws the above exception.
I think the issue might come from using a dataclass without using the correct mapping method. There are three ways to apply ORM Mappings to a dataclass. You seem to use declarative mapping so I'll just apply the declarative mapping method for SQLAlchemy 1.4.
#mapper_registry.mapped
#dataclass
class User:
__tablename__ = "users"
__table_args__ = {'extend_existing': True}
__sa_dataclass_metadata_key__ = "sa"
email: str | None = field(init=False, metadata={"sa": Column(String, unique=True)})
username: str | None = field(init=False, metadata={"sa": Column(String, unique=True)})
f_name: str | None = field(init=False, metadata={"sa": Column(String)})
l_name: str | None = field(init=False, metadata={"sa": Column(String)})
m_name: str | None = field(init=False, metadata={"sa": Column(String)})
role_id: int | None = field(init=False, metadata={"sa": Column(Integer, ForeignKey('roles.id'))})
#user_role = relationship('app.modules.roles.role_models.Role', backref="users")
# A user can have access to many projects where they are not the owners of the project.
#projects = relationship('app.modules.projects.project_models.Project', secondary="project_users", back_populates="users")
projects: list[Project] = field(default_factory=list, metadata={"sa": relationship('app.modules.projects.project_models.ProjectUsers', back_populates='user')})
# define repr by using the dataclasses.field repr kwarg
If that alone does not solve your problem, please add the definition of app.modules.database.BaseModel to your question.

validate column of polymorphic table

I have a flask-sqlalchemy polymorphic table structure like so
class Parent(db.Model):
id = db.Column(db.Integer, primary_key=True)
polytype = db.Column(db.String(32), nullable=False)
value = db.Column(db.String(32))
__mapper_args__ = {'polymorphic_identity': 'parent',
'polymorphic_on': polytype}
class Child(Parent):
id = db.Column(db.Integer,db.ForeignKey('parent.id'),
primary_key=True)
#validates('value')
def validate_value(self, key, val):
# [validation code]
return value
__mapper_args__ = {'polymorphic_identity': 'child'}
and I want to validate the value field. However, the validator for Child.value, the column inherited from Parent, never runs.
What is the correct way to validate an inherited column?
There's an old open issue about it.
In that issue, it is suggested that using an event listener can work, for example:
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import event
db = SQLAlchemy()
class Parent(db.Model):
id = db.Column(db.Integer, primary_key=True)
polytype = db.Column(db.String(32), nullable=False)
value = db.Column(db.String(32))
__mapper_args__ = {'polymorphic_identity': 'parent',
'polymorphic_on': polytype}
class Child(Parent):
id = db.Column(db.Integer,db.ForeignKey('parent.id'),
primary_key=True)
__mapper_args__ = {'polymorphic_identity': 'child'}
#event.listens_for(Parent.value, "set", propagate=True)
def validate_value(inst, val, *args):
print(f"checking value for {inst}")
assert val == "spam"
Parent(value="spam")
Child(value="spam")
If you don't want the listener to fire on Parent instances, decorate your listener func with event.listens_for(Child.value, ...).
Another workaround for this known open issue is creating an instance method to be called inside the #validate method, for example:
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.orm import validates
from sqlalchemy import event
db = SQLAlchemy()
class Parent(db.Model):
id = db.Column(db.Integer, primary_key=True)
polytype = db.Column(db.String(32), nullable=False)
value = db.Column(db.String(32))
__mapper_args__ = {'polymorphic_identity': 'parent',
'polymorphic_on': polytype}
#validates('value')
def validate_value(self, key, new_value):
self._validate_value(new_value)
def _validate_value(self, new_value):
print(f"checking value for {self}")
assert new_value == "spam"
class Child(Parent):
id = db.Column(db.Integer,db.ForeignKey('parent.id'),
primary_key=True)
__mapper_args__ = {'polymorphic_identity': 'child'}
def _validate_value(self, new_value):
print(f"checking value for {self}")
assert new_value == "child"
Parent(value="spam")
Child(value="child")
In this case, you are able to validate the value having different behaviors on the Childs overwriting the private method _validate_value

Polymorphic filtering on hybrid properties when inheriting from AbstractConcreteBase

I've had success polymorphically loading items via an AbstractConcreteBase parent. Is it possible to also filter on hybrid_property definitions of descendants? I'm also wanting to filter on different columns per child class.
Queries
# Fetching all ItemA and ItemB works well with...
ItemBase.query.all()
# Given the models below is it possible to filter on the children's
# item_id hybrid_property to fetch all ItemB with item_b_id of 1
# Result is []
ItemBase.query.filter(ItemBase.item_id == 'B1')
# This also doesn't work
# Result is everything unfiltered
ItemBase.query.filter(ItemB.item_id == 'B1')
Models:
from sqlalchemy.sql.expression import cast
from sqlalchemy.ext.declarative import AbstractConcreteBase
from sqlalchemy.ext.hybrid import hybrid_property
class ItemBase(AbstractConcreteBase, Base):
__tablename__ = None
#hybrid_property
def item_id(self): pass
#activity_id.expression
def item_id(cls):
pass
class ItemA(ItemBase):
__tablename__ = 'item_a'
__mapper_args__ = {
'polymorphic_identity': 'item_a',
'concrete':True
}
item_a_id = db.Column(db.Integer, primary_key=True)
#hybrid_property
def item_id(self):
return 'A' + str(self.item_a_id)
#activity_id.expression
def item_id(cls):
return 'A' + str(self.item_a_id)
class ItemB(ItemBase):
__tablename__ = 'item_b'
__mapper_args__ = {
'polymorphic_identity': 'item_b',
'concrete':True
}
item_b_id = db.Column(db.Integer, primary_key=True)
#hybrid_property
def item_id(self):
return 'B' + str(self.item_b_id)
#activity_id.expression
def item_id(cls):
return 'B' + str(self.item_b_id)
I'm stuck with the table structure for now. Any help is greatly appreciated.

Django Integrity Error - id cannot be null

I know this has been answered before, but I have been through every answer and nothing either makes sense or I feel like my code is already doing what is being said in the answers.
So I have a model for terms and links, which has a many-to-many relationship.
class ProjectTerms(models.Model):
id = models.IntegerField(primary_key=True) # AutoField?
terms = models.CharField(max_length=100)
class Meta:
db_table = 'project_terms'
class ProjectLinks(models.Model):
id = models.IntegerField(primary_key=True) # AutoField?
links = models.CharField(max_length=100)
relations = models.ManyToManyField(ProjectTerms)
class Meta:
db_table = 'project_links'
class ProjectLinksRelations(models.Model):
id = models.IntegerField(primary_key=True) # AutoField?
project_terms = models.ForeignKey('ProjectTerms')
project_links = models.ForeignKey(ProjectLinks)
Script:
def create_models(my_term, my_link):
saved_term = ProjectTerms.objects.update_or_create(terms = my_term)
saved_link = ProjectLinks.objects.update_or_create(links = my_link)
ProjectLinksRelations.objects.update_or_create(project_terms=saved_term, project_links=saved_link)
A lot of places say that the model (both term and link here) has to be saved before being added to the connecting database.
I've also tried creating each term and link separately and saving them like this:
def create_models(my_term, my_link):
saved_term = ProjectTerms(terms = my_term)
saved_term.save()
saved_link = ProjectLinks(links = my_link)
saved_link.save()
relation = ProjectLinksRelations.objects.update_or_create(project_terms=saved_term, project_links=saved_link)
relation.save()
All you need for this is:
class ProjectTerms(models.Model):
terms = models.CharField(max_length=100)
class ProjectLinks(models.Model):
links = models.CharField(max_length=100)
relations = models.ManyToManyField(ProjectTerms)
The ID fields and the table for the ManyToManyField will be automatically created.
To set the ManyToMany relationship, use add:
def create_models(my_term, my_link):
(saved_term, created) = ProjectTerms.objects.update_or_create(terms = my_term)
(saved_link, created) = ProjectLinks.objects.update_or_create(links = my_link)
saved_link.relations.add(saved_term)
saved_link.save()

sqlalchemy multiple databases with same table names not working

I have two databases that I'm working with in with Python using SQLAlchemy, the databases share table names and therefore I'm getting an error message when running the code.
The error message is :
sqlalchemy.exc.InvalidRequestError: Table 'wo' is already defined for this MetaData instance. Specify 'extend_existing=True' to redefine options and columns on an existing Table object.
The simplified code is below:
from sqlalchemy import create_engine, Column, Integer, String, DateTime, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, relationship, backref
from mysql.connector.connection import MySQLConnection
Base = declarative_base()
def get_characterset_info(self):
return self.get_charset()
MySQLConnection.get_characterset_info = MySQLConnection.get_charset
mysqlengine = create_engine('mysql+mysqlconnector://......../mp2', echo=True)
MYSQLSession = sessionmaker(bind=mysqlengine)
mysqlsession= MYSQLSession()
MP2engine = create_engine('mssql+pyodbc://......../mp2', echo=True)
MP2Session = sessionmaker(bind=MP2engine)
mp2session= MP2Session()
class MYSQLWo(Base):
__tablename__= 'wo'
wonum = Column(String, primary_key=True)
taskdesc = Column(String)
comments = relationship("MYSQLWocom", order_by="MYSQLWocom.wonum", backref='wo')
class MYSQLWocom (Base):
__tablename__='wocom'
wonum = Column(String, ForeignKey('wo.wonum'), primary_key=True)
comments = Column(String, primary_key=True)
class MP2Wo(Base):
__tablename__= 'wo'
wonum = Column(String, primary_key=True)
taskdesc = Column(String)
comments = relationship("MP2Wocom", order_by="MP2Wocom.wonum", backref='wo')
class MP2Wocom (Base):
__tablename__='woc'
wonum = Column(String, ForeignKey('wo.wonum'), primary_key=True)
location = Column(String)
sublocation1 = Column(String)
texts = Column(String, primary_key=True)
How do I deal with databases having the same table structure? I'm guessing it has something to do with the MetaData instance, but the SQLAlchemy documentation gets a little confusing when talking about the difference in the class declarative and classical usage..
Since in reality the tables had different structures, the solution was to simply create a separate declarative base. If the tables indeed had the same structure, I would have only needed one class for both tables.
Base = declarative_base()
Base2 = declarative_base() #this is all I needed
class MYSQLWo(Base):
....
class MYSQLWocom(Base):
....
class MP2Wo(Base2):
....
class MP2Wocom(Base2)
http://groups.google.com/group/sqlalchemy/browse_thread/thread/afe09d6387a4dc69?hl=en
You can use one db instance with two Model to bypass this problem.
And this can also be used to implement a master/slave use case in Flask-SQLAlchemy.
Just like this:
app = Flask(__name__)
app.config['SQLALCHEMY_BINDS'] = {'rw': 'rw', 'r': 'r'}
db = SQLAlchemy(app)
db.Model_RW = db.make_declarative_base()
class A(db.Model):
__tablename__ = 'common'
class B(db.Model_RW):
__tablename__ = 'common'