hi -
this is perhaps a case automap should come up with something easier for,
and at least have a note in the docs that refers to this specifically,
however the general approach is to apply a naming convention to the
relationships that will allow the conflict to resolve. you'd define
name_for_collection_relationship, following th guidelines in
the section:
http://docs.sqlalchemy.org/en/latest/orm/extensions/automap.html?highlight=automap#handling-simple-naming-conflicts
def name_for_collection_relationship(base, local_cls, referred_cls,
constraint):
disc = constraint.columns[0].name
return referred_cls.__name__.lower() + disc + "_collection"
On 04/02/2017 02:32 PM, yoch.me...@gmail.com wrote:
Hi,
I want to use automap to generate mapping from existing database, but
the relation mapping fails.
I understand what happens, but I can't find a proper workaround to fix
this error.
Here a minimal code example with the minimal schema :
CREATE TABLE `user` (
`id` INT UNSIGNED NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB;
CREATE TABLE `contact` (
`iduser` INT UNSIGNED NOT NULL,
`idcontact` INT UNSIGNED NOT NULL,
PRIMARY KEY (`iduser`, `idcontact`),
FOREIGN KEY (`iduser`) REFERENCES `user` (`id`),
FOREIGN KEY (`idcontact`) REFERENCES `user` (`id`)
) ENGINE=InnoDB;
The minimal code is :
Base = automap_base()
Base.prepare(engine, reflect=True)
configure_mappers()
which fails with this backtrace :
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "testing.py", line 11, in <module>
configure_mappers()
File "../site-packages/sqlalchemy/orm/mapper.py", line 2866, in
configure_mappers
mapper._post_configure_properties()
File "../site-packages/sqlalchemy/orm/mapper.py", line 1765, in
_post_configure_properties
prop.init()
File "../site-packages/sqlalchemy/orm/interfaces.py", line 184, in init
self.do_init()
File "../site-packages/sqlalchemy/orm/relationships.py", line 1656, in
do_init
self._generate_backref()
File "../site-packages/sqlalchemy/orm/relationships.py", line 1837, in
_generate_backref
(backref_key, self, m))
sqlalchemy.exc.ArgumentError: Error creating backref 'user_collection'
on relationship 'user.user_collection': property of that name exists on
mapper 'Mapper|user|user'
In fact, I only want to have one-to-many relationship that connects a
user to all his contacts. But Sqlalchemy try to make a bidirectional
relationship (many-to-many), which fails because the relationship name
is the same in both cases (user_collection).
Do you have a good solution for this problem ?
Best regards
--
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 sqlalchemy+unsubscr...@googlegroups.com
<mailto:sqlalchemy+unsubscr...@googlegroups.com>.
To post to this group, send email to sqlalchemy@googlegroups.com
<mailto:sqlalchemy@googlegroups.com>.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.
--
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 sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.