Hi all
I've updated an old sql alchemy powered application and I'm struggling with
a relationship issue.
Having this code:
class Member(object):
_data_provider = inject.attr(DataProvider)
def __init__(self, id_member=None, name=None):
self.id_member = id_member
self.name = name
self.alocs = []
*def save(self):*
* auto_projects = self._data_provider.list_projects_aloc_auto()*
* self.alocs = [Aloca(project_id=project.id, member.id=self.id,
date=datetime.datetime.now()) for project in auto_projects]*
* self._data_provider.save_member(self)*
class Project(object):
_data_provider = inject.attr(DataProvider)
def __init__(self, id, name):
self.id = id
self.name = name
self.places = []
class Aloc(object):
_data_provider = inject.attr(DataProvider)
def __init__(self, id, id_project, date):
self.id = id
self.id_project = id_project
self.date = date
class AutoAloc(Aloc):
pass
class Manager(Aloc):
pass
from sqlalchemy.schema import MetaData, Table, Column, ForeignKey
class DataProvider(object):
_metadata = MetaData()
_engine = None
_SessionFactory = sessionmaker(expire_on_commit=False) # Factory de
sessões
def _map_aloc(self):
alocs = Table("aloc", self._metadata,
Column("id", Integer, primary_key=True),
Column("id_project", Integer, ForeignKey("project.id")),
Column("date", DateTime),
mysql_engine='InnoDB',
mysql_charset='utf8')
return mapper(Aloc, alocs,
polymorphic_on=alocs.c.type,
polymorphic_identity="aloc",
)
def _map_manager(self, aloc_mapper):
return mapper(Manager, inherits=aloc_mapper,
polymorphic_identity='manager')
def _map_aloc_auto(self, aloc_mapper):
return mapper(AlocAuto, inherits=alocacao_mapper,
polymorphic_identity='alocauto')
def _map_project(self):
projects = Table("project", self._metadata,
Column("id", Integer, primary_key=True),
Column("name", String(255)),
mysql_engine='InnoDB',
mysql_charset='utf8')
return mapper(Project, projects,
"managers": relationship(Manager),
"alocs": relationship(Aloc, backref="project", cascade="all,
delete-orphan")})
def _map_member(self):
members = Table("member", self._metadata,
Column("id", Integer, primary_key=True),
Column("name", String(255)),
mysql_engine='InnoDB',
mysql_charset='utf8')
return mapper(Member, members,
polymorphic_on=members.c.type,
polymorphic_identity="member",
alocs": relationship(Aloc,
backref="member", cascade="all,delete- orphan"),
})
def __init__(self, connection_string, connection_pool_size,
disable_overflow=True):
if DataProvider._engine is None:
if disable_overflow:
DataProvider._engine = create_engine(connection_string,
max_overflow=0, pool_size=connection_pool_size)
else:
DataProvider._engine = create_engine(connection_string,
pool_size=connection_pool_size)
DataProvider._SessionFactory.configure(bind=self._engine)
self._map_project()
self._map_member()
aloc_mapper = self._map_aloc()
self._map_aloc_auto(aloc_mapper)
self._map_manager(aloc_mapper)
*def save_member(self, member):*
* self._session.add(member)*
* return member*
Of course, more fields and methods in all classes...
The issue is, when adding a new Member, *member.alocs* get's filled with
some objects.
It creates the record in DB but project_id is null.
I receive this debug message:
/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/relationships.py:3441:
SAWarning: relationship 'Aloc.project' will copy column project.id to
column aloc.project_id, which conflicts with relationship(s):
'Manager.manager
es' (copies project.id to aloc.project_id). If this is not the intention,
consider if these relationships should be linked with back_populates, or if
viewonly=True should be applied to one or more if they are read-only. For
the le
ss common case that foreign key constraints are partially overlapping, the
orm.foreign() annotation can be used to isolate the columns that should be
written towards. The 'overlaps' parameter may be used to remove this
warning. (Backg
round on this error at: http://sqlalche.me/e/14/qzyx)
util.warn(
/usr/local/lib/python3.8/site-packages/sqlalchemy/orm/relationships.py:3441:
SAWarning: relationship 'Project.aloc' will copy column project.id to
column aloc.project_id, which conflicts with relationship(s):
'Project.managers' (copies project.id to aloc.project_id). If this is not
the intention, consider if these relationships should be linked with
back_populates, or if viewonly=True should be applied to one or more if
they are read-only. For the l
ess common case that foreign key constraints are partially overlapping, the
orm.foreign() annotation can be used to isolate the columns that should be
written towards. The 'overlaps' parameter may be used to remove this
warning. (Back
ground on this error at: http://sqlalche.me/e/14/qzyx)
Please consider that I might mistaken myself in project_id. Might be
id_project and typos like those.
Please. any idea?
kind regards,
Emanuel
--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
http://www.sqlalchemy.org/
To post example code, please provide an MCVE: Minimal, Complete, and Verifiable
Example. See http://stackoverflow.com/help/mcve for a full description.
---
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/sqlalchemy/ded1ffbe-0244-41fc-aa19-9d46630285a5n%40googlegroups.com.