I have a similar issue. I adjusted the name_for_scalar_relationship function as described here: http://docs.sqlalchemy.org/en/latest/orm/extensions/automap.html#handling-simple-naming-conflicts
The odd thing is that name_for_scalar_relationship is executed only once for the last foreign key constraint, but not for the first. I don't think this is normal behavior? I am using python3 and current postgres DB. On Wednesday, 6 January 2016 11:43:54 UTC+1, yoch....@gmail.com wrote: > > For now, I don't have a sufficiently comprehensive view of the automap > process and its effects to make a pull-request. > Maybe one day I'll take time to learn more about that, and to understand > the corresponding tests. > > Thank you > > Le lundi 4 janvier 2016 05:19:17 UTC+2, Michael Bayer a écrit : >> >> there's actually two if you put your check everywhere it would be needed >> (I already tried when you first suggested that it warn, which is the >> origin of my response that this is not a one liner). >> >> the warning here is doable it just needs more state to be tracked as >> automap runs. if you want to work on this I can accept a pull request >> if you can add some tests. >> >> >> >> On 01/03/2016 07:24 PM, yoch....@gmail.com wrote: >> > Okay, I have one test failed for automap : >> > >> > ~/sqlalchemy $ ./sqla_nose.py test.ext.test_automap >> > .....E...... >> > ====================================================================== >> > ERROR: >> > >> test.ext.test_automap.AutomapTest.test_relationship_explicit_override_m2o >> > ---------------------------------------------------------------------- >> > Traceback (most recent call last): >> > File "/usr/lib/python2.7/dist-packages/nose/case.py", line 197, in >> runTest >> > self.test(*self.arg) >> > File "/home/pi/sqlalchemy/test/ext/test_automap.py", line 55, in >> > test_relationship_explicit_override_m2o >> > Base.prepare() >> > File "/home/pi/sqlalchemy/lib/sqlalchemy/ext/automap.py", line 777, >> in >> > prepare >> > generate_relationship) >> > File "/home/pi/sqlalchemy/lib/sqlalchemy/ext/automap.py", line 956, >> in >> > _relationships_for_fks >> > util.warn(msg) >> > File "/home/pi/sqlalchemy/lib/sqlalchemy/util/langhelpers.py", line >> > 1292, in warn >> > warnings.warn(msg, exc.SAWarning, stacklevel=2) >> > SAWarning: Address relationship name conflict: users >> > >> > ---------------------------------------------------------------------- >> > Ran 12 tests in 27.450s >> > >> > FAILED (errors=1) >> > >> > Le lundi 4 janvier 2016 01:06:42 UTC+2, Michael Bayer a écrit : >> > >> > >> > >> > On 01/03/2016 02:43 PM, yoch....@gmail.com <javascript:> wrote: >> > > I've added theses lines here >> > > >> > < >> https://bitbucket.org/zzzeek/sqlalchemy/src/c7d6c667b53d96a65e0dedcb83c098e03d4c7453/lib/sqlalchemy/ext/automap.py?at=master&fileviewer=file-view-default#automap.py-953 >> >> > < >> https://bitbucket.org/zzzeek/sqlalchemy/src/c7d6c667b53d96a65e0dedcb83c098e03d4c7453/lib/sqlalchemy/ext/automap.py?at=master&fileviewer=file-view-default#automap.py-953>> >> >> >> > >> > > : >> > > >> > > | >> > > ifrelationship_name inmap_config.properties: >> > > msg ="%s relationship name conflict: >> > > %s"%(local_cls.__name__,relationship_name) >> > > util.warn(msg) >> > > | >> > > >> > > and it produces warnings as excepted : >> > > >> > >>>> from dbmodels import * >> > > >> /usr/local/lib/python2.7/dist-packages/sqlalchemy/ext/automap.py:961: >> > > SAWarning: *thermostats relationship name conflict: >> dispositif_ref* >> > > util.warn(msg) >> > > >> > > Whats wrong with this approach ? Is there any case that overwrite >> > > relationship_name in map_config.properties is correct ? >> > >> > please run the test suite and observe the tests that fail with this >> > approach, specifically those which test the behavior of being able >> to >> > specify an explicit mapping with existing relationships. >> > >> > >> > >> > >> > > >> > > >> > > Le dimanche 3 janvier 2016 19:09:04 UTC+2, Michael Bayer a écrit >> : >> > > >> > > >> > > >> > > On 01/03/2016 07:00 AM, yoch....@gmail.com <javascript:> >> wrote: >> > > > OK, thanks you. >> > > > >> > > > I think it's a good idea to issue a warning in such cases. >> > > >> > > unfortunately this is a difficult situation to detect since >> it >> > is a >> > > valid use case to present a mapped class that already has >> > relationships >> > > present on it, which will not be overridden. Additional >> > bookkeeping >> > > would need to be added to the automap process to track all >> > those class/ >> > > name combinations that were locally added without coming up >> > with false >> > > positives. >> > > >> > > >> > > >> > > > >> > > > Best regards >> > > > >> > > > Le samedi 2 janvier 2016 19:18:12 UTC+2, Michael Bayer a >> > écrit : >> > > > >> > > > >> > > > >> > > > On 01/02/2016 11:38 AM, yoch....@gmail.com >> <javascript:> >> > wrote: >> > > > > Thank you. >> > > > > >> > > > > I hesitate between using this way, or explicitly >> > specify the >> > > > relationship >> > > > > (is this a good idea? In my test I found 3 relations >> > after >> > > > prepare()) : >> > > > >> > > > it's fine to do that. Automap is still building its >> own >> > > > relationship as >> > > > well which is why you end up with three of them. The >> > > > generate_relationship hook can be used to return None >> in >> > those >> > > cases >> > > > where you don't want automap to generate a >> relationship: >> > > > >> > > >> > >> http://docs.sqlalchemy.org/en/rel_1_0/orm/extensions/automap.html#sqlalchemy.ext.automap.generate_relationship >> >> > < >> http://docs.sqlalchemy.org/en/rel_1_0/orm/extensions/automap.html#sqlalchemy.ext.automap.generate_relationship> >> >> >> > >> > > >> > < >> http://docs.sqlalchemy.org/en/rel_1_0/orm/extensions/automap.html#sqlalchemy.ext.automap.generate_relationship >> >> > < >> http://docs.sqlalchemy.org/en/rel_1_0/orm/extensions/automap.html#sqlalchemy.ext.automap.generate_relationship>> >> >> >> > >> > > >> > > > >> > > >> > < >> http://docs.sqlalchemy.org/en/rel_1_0/orm/extensions/automap.html#sqlalchemy.ext.automap.generate_relationship >> >> > < >> http://docs.sqlalchemy.org/en/rel_1_0/orm/extensions/automap.html#sqlalchemy.ext.automap.generate_relationship> >> >> >> > >> > > >> > < >> http://docs.sqlalchemy.org/en/rel_1_0/orm/extensions/automap.html#sqlalchemy.ext.automap.generate_relationship >> >> > < >> http://docs.sqlalchemy.org/en/rel_1_0/orm/extensions/automap.html#sqlalchemy.ext.automap.generate_relationship>>> >> >> >> > >> > > >> > > > >> > > > >> > > > >> > > > > >> > > > > | >> > > > > classThermostat(Base): >> > > > > __tablename__ ='thermostats' >> > > > > idbuiltin =Column(Integer,ForeignKey('device.id >> > <http://device.id> >> > > <http://device.id> >> > > > <http://device.id>')) >> > > > > idthermometer >> > =Column(Integer,ForeignKey('device.id <http://device.id> >> > > <http://device.id> >> > > > <http://device.id>')) >> > > > > thermometer >> > > =relationship(Dispositif,foreign_keys=idthermometer) >> > > > > builtin >> > =relationship(Dispositif,foreign_keys=idbuiltin) >> > > > > | >> > > > > >> > > > > >> > > > > Another interesting point is how to detect this error >> to >> > > warn. I >> > > > tried >> > > > > to use name_for_scalar_relationship() for that, but I >> > don't kow >> > > > how to >> > > > > get the relationships mapper. >> > > > > >> > > > > Best regards >> > > > > >> > > > > Le vendredi 1 janvier 2016 18:27:14 UTC+2, Michael >> > Bayer a >> > > écrit : >> > > > > >> > > > > you need to use the name generation functions >> > > > > name_for_scalar_relationship() and/or >> > > > > name_for_collection_relationship() >> > > > > to produce different names in each case. The >> > "constraint" >> > > > parameter >> > > > > passed as we see in >> > > > > >> > > > >> > > >> > >> http://docs.sqlalchemy.org/en/rel_1_0/orm/extensions/automap.html#handling-simple-naming-conflicts >> >> > < >> http://docs.sqlalchemy.org/en/rel_1_0/orm/extensions/automap.html#handling-simple-naming-conflicts> >> >> >> > >> > > >> > < >> http://docs.sqlalchemy.org/en/rel_1_0/orm/extensions/automap.html#handling-simple-naming-conflicts >> >> > < >> http://docs.sqlalchemy.org/en/rel_1_0/orm/extensions/automap.html#handling-simple-naming-conflicts>> >> >> >> > >> > > >> > > > >> > > >> > < >> http://docs.sqlalchemy.org/en/rel_1_0/orm/extensions/automap.html#handling-simple-naming-conflicts >> >> > < >> http://docs.sqlalchemy.org/en/rel_1_0/orm/extensions/automap.html#handling-simple-naming-conflicts> >> >> >> > >> > > >> > < >> http://docs.sqlalchemy.org/en/rel_1_0/orm/extensions/automap.html#handling-simple-naming-conflicts >> >> > < >> http://docs.sqlalchemy.org/en/rel_1_0/orm/extensions/automap.html#handling-simple-naming-conflicts>>> >> >> >> > >> > > >> > > > >> > > > > >> > > > >> > > >> > < >> http://docs.sqlalchemy.org/en/rel_1_0/orm/extensions/automap.html#handling-simple-naming-conflicts >> >> > < >> http://docs.sqlalchemy.org/en/rel_1_0/orm/extensions/automap.html#handling-simple-naming-conflicts> >> >> >> > >> > > >> > < >> http://docs.sqlalchemy.org/en/rel_1_0/orm/extensions/automap.html#handling-simple-naming-conflicts >> >> > < >> http://docs.sqlalchemy.org/en/rel_1_0/orm/extensions/automap.html#handling-simple-naming-conflicts>> >> >> >> > >> > > >> > > > >> > > >> > < >> http://docs.sqlalchemy.org/en/rel_1_0/orm/extensions/automap.html#handling-simple-naming-conflicts >> >> > < >> http://docs.sqlalchemy.org/en/rel_1_0/orm/extensions/automap.html#handling-simple-naming-conflicts> >> >> >> > >> > > >> > < >> http://docs.sqlalchemy.org/en/rel_1_0/orm/extensions/automap.html#handling-simple-naming-conflicts >> >> > < >> http://docs.sqlalchemy.org/en/rel_1_0/orm/extensions/automap.html#handling-simple-naming-conflicts>>>> >> >> >> > >> > > >> > > > >> > > > > >> > > > > is a ForeignKeyConstraint object, you can look >> > inside of >> > > > > constraint.column_keys to see if it is >> > ['idbuiltin'] or >> > > > > ['idthermometer'] and use that to generate a >> name. >> > > > > >> > > > > >> > > > > >> > > > > On 01/01/2016 04:16 AM, yoch....@gmail.com >> > <javascript:> >> > > wrote: >> > > > > > Hi all, >> > > > > > >> > > > > > I use automap with database reflection to >> import >> > > schema with >> > > > > sqlalchemy. >> > > > > > >> > > > > > In case I have two relationships on same >> foreign >> > key >> > > in some >> > > > > table, only >> > > > > > one relationship is created by prepare(), the >> > second >> > > one seems >> > > > > overwrited. >> > > > > > >> > > > > > My table looks like : >> > > > > > >> > > > > > | >> > > > > > Table('thermostat', >> > > > > > Base.metadata, >> > > > > > >> > > Column('id',INTEGER(),primary_key=True,nullable=False), >> > > > > > >> > Column('idbuiltin',INTEGER(),ForeignKey('device.id < >> http://device.id> >> > > <http://device.id> >> > > > <http://device.id> >> > > > > <http://device.id>')), >> > > > > > >> > > Column('idthermometer',INTEGER(),ForeignKey('device.id >> > <http://device.id> >> > > <http://device.id> >> > > > <http://device.id> >> > > > > <http://device.id>'))) >> > > > > > | >> > > > > > >> > > > > > How to control relationship creation to produce >> two >> > > distinct >> > > > > relationships ? >> > > > > > >> > > > > > Thank you and Happy New Year ! >> > > > > > >> > > > > > -- >> > > > > > 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+...@googlegroups.com >> > <javascript:> >> > > > > > <mailto:sqlalchemy+unsubscr...@googlegroups.com >> > <javascript:> >> > > <javascript:> >> > > > <javascript:> <javascript:>>. >> > > > > > To post to this group, send email to >> > > sqlal...@googlegroups.com >> > > > > <javascript:> >> > > > > > <mailto:sqlal...@googlegroups.com >> <javascript:>>. >> > > > > > Visit this group at >> > > > https://groups.google.com/group/sqlalchemy >> > <https://groups.google.com/group/sqlalchemy> >> > > <https://groups.google.com/group/sqlalchemy >> > <https://groups.google.com/group/sqlalchemy>> >> > > > <https://groups.google.com/group/sqlalchemy >> > <https://groups.google.com/group/sqlalchemy> >> > > <https://groups.google.com/group/sqlalchemy >> > <https://groups.google.com/group/sqlalchemy>>> >> > > > > <https://groups.google.com/group/sqlalchemy >> > <https://groups.google.com/group/sqlalchemy> >> > > <https://groups.google.com/group/sqlalchemy >> > <https://groups.google.com/group/sqlalchemy>> >> > > > <https://groups.google.com/group/sqlalchemy >> > <https://groups.google.com/group/sqlalchemy> >> > > <https://groups.google.com/group/sqlalchemy >> > <https://groups.google.com/group/sqlalchemy>>>>. >> > > > > > For more options, visit >> > > https://groups.google.com/d/optout >> > <https://groups.google.com/d/optout> >> > <https://groups.google.com/d/optout >> > <https://groups.google.com/d/optout>> >> > > > <https://groups.google.com/d/optout >> > <https://groups.google.com/d/optout> >> > > <https://groups.google.com/d/optout >> > <https://groups.google.com/d/optout>>> >> > > > > <https://groups.google.com/d/optout >> > <https://groups.google.com/d/optout> >> > > <https://groups.google.com/d/optout >> > <https://groups.google.com/d/optout>> >> > > > <https://groups.google.com/d/optout >> > <https://groups.google.com/d/optout> >> > > <https://groups.google.com/d/optout >> > <https://groups.google.com/d/optout>>>>. >> > > > > >> > > > > -- >> > > > > 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+...@googlegroups.com >> <javascript:> >> > > > > <mailto:sqlalchemy+unsubscr...@googlegroups.com >> > <javascript:> >> > > <javascript:> <javascript:>>. >> > > > > To post to this group, send email to >> > sqlal...@googlegroups.com >> > > > <javascript:> >> > > > > <mailto:sqlal...@googlegroups.com <javascript:>>. >> > > > > Visit this group at >> > > https://groups.google.com/group/sqlalchemy >> > <https://groups.google.com/group/sqlalchemy> >> > > <https://groups.google.com/group/sqlalchemy >> > <https://groups.google.com/group/sqlalchemy>> >> > > > <https://groups.google.com/group/sqlalchemy >> > <https://groups.google.com/group/sqlalchemy> >> > > <https://groups.google.com/group/sqlalchemy >> > <https://groups.google.com/group/sqlalchemy>>>. >> > > > > For more options, visit >> > https://groups.google.com/d/optout < >> https://groups.google.com/d/optout> >> > > <https://groups.google.com/d/optout >> > <https://groups.google.com/d/optout>> >> > > > <https://groups.google.com/d/optout >> > <https://groups.google.com/d/optout> >> > > <https://groups.google.com/d/optout >> > <https://groups.google.com/d/optout>>>. >> > > > >> > > > -- >> > > > 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+...@googlegroups.com <javascript:> >> > > > <mailto:sqlalchemy+unsubscr...@googlegroups.com >> > <javascript:> <javascript:>>. >> > > > To post to this group, send email to >> sqlal...@googlegroups.com >> > > <javascript:> >> > > > <mailto:sqlal...@googlegroups.com <javascript:>>. >> > > > Visit this group at >> > https://groups.google.com/group/sqlalchemy >> > <https://groups.google.com/group/sqlalchemy> >> > > <https://groups.google.com/group/sqlalchemy >> > <https://groups.google.com/group/sqlalchemy>>. >> > > > For more options, visit https://groups.google.com/d/optout >> > <https://groups.google.com/d/optout> >> > > <https://groups.google.com/d/optout >> > <https://groups.google.com/d/optout>>. >> > > >> > > -- >> > > 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+...@googlegroups.com <javascript:> >> > > <mailto:sqlalchemy+unsubscr...@googlegroups.com <javascript:>>. >> > > To post to this group, send email to sqlal...@googlegroups.com >> > <javascript:> >> > > <mailto:sqlal...@googlegroups.com <javascript:>>. >> > > Visit this group at https://groups.google.com/group/sqlalchemy >> > <https://groups.google.com/group/sqlalchemy>. >> > > For more options, visit https://groups.google.com/d/optout >> > <https://groups.google.com/d/optout>. >> > >> > -- >> > 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+...@googlegroups.com >> > <mailto:sqlalchemy+unsubscr...@googlegroups.com>. >> > To post to this group, send email to sqlal...@googlegroups.com >> > <mailto:sqlal...@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.