Hello -

For future issues of this nature, please take care to include the table definition that is being reflected, so that others can help to answer this question. I had to deduce backwards what the problem would be here, it is a table of this form:

engine.execute("""
    create table item (
        id integer,
        primary key (id)
    )
""")
engine.execute("""
    create table item_to_item (
        item_a_id integer,
        item_b_id integer,
        primary key (item_a_id, item_b_id),
        foreign key (item_a_id) references item(id),
        foreign key (item_b_id) references item(id)
    )
""")


You need to use a naming convention to break the name overlap (e.g. http://docs.sqlalchemy.org/en/latest/orm/extensions/automap.html#overriding-naming-schemes):

def make_collection_name(base, local_cls, referred_cls, constraint):
    if local_cls is referred_cls:
        return constraint.columns.keys()[0] + "_collection"


this would produce collections of the name "item_a_id_collection" and "item_b_id_collection". You might want to hardcode a lookup table with more convenient names.




On 01/18/2017 09:40 PM, Liu Shengpeng wrote:
Base = automap_base()
engine = create_engine("sqlite:///xx.sqlite")

Base.prepare(engine, reflect=True)
session = Session(engine)

Item = Base.classes["items"]

for item in session.query(Item).limit(10):
    print(item)


error occurs:

InvalidRequestError: One or more mappers failed to initialize - can't
proceed with initialization of other mappers. Triggering mapper:
'Mapper|items|items'. Original exception was: Error creating backref
'items_collection' on relationship 'items.items_collection': property of
that name exists on mapper 'Mapper|items|items'


--
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]
<mailto:[email protected]>.
To post to this group, send email to [email protected]
<mailto:[email protected]>.
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to