Hi there,
I have a bunch of mysql tables.
Up to now I did not define their structure in python but used
Base.metadata.reflect(engine)
No I started to define their structure in Python and stumbled over the fact, that I do not understand
how ForeignKeyConstraint should be used correctly.

The following tables
tblPerson, tblFlag
are linked using the association table tblPerson_has_Flag.

this works fine if I explicitly define columns to be part of foreign keys as shown in tblPerson_has_Flag below.
what I wanted to do, but failed was:
in tblPerson_has_Flag insted of marking the columns to be part of a fk to have the following construct
    __table_args__ = (
        sa.ForeignKeyConstraint(
            ['tblPerson_id'], [u'tblPerson.id'],
            ['tblFlag_id'], [u'tblFlag.id'],
        ),
        sa.PrimaryKeyConstraint(u'tblPerson_id', u'tblFlag_id'), {}
    )
However, if I do so, I get an sqlalchemy.exc.NoForeignKeysError: Could not determine join condition between parent/child
error

how should I use ForeignKeyConstraint correctly?

thanks
robert

## ----------------------------------------------------------------------------
## flags
## ----------------------------------------------------------------------------
class tblPerson(Base):
    __tablename__ = 'tblPerson'
    id = sa.Column( mysql.INTEGER(display_width=11), nullable=False)
    lastname = sa.Column( mysql.VARCHAR(length=124), nullable=False)
firstname = sa.Column( mysql.VARCHAR(length=45), server_default='', nullable=True)
    ...
    __table_args__ = (
        sa.PrimaryKeyConstraint(u'id'), {}
    )

class tblPerson_has_Flag(Base):
    __tablename__ = 'tblPerson_has_Flag'
    tblPerson_id = sa.Column( mysql.INTEGER(display_width=11),
        sa.ForeignKey('tblPerson.id'),
        autoincrement=False, nullable=False)
    tblFlag_id = sa.Column( mysql.INTEGER(display_width=11),
        sa.ForeignKey('tblFlag.id'),
        autoincrement=False, nullable=False)
    __table_args__ = (
        #sa.ForeignKeyConstraint(
            #['tblPerson_id'], [u'tblPerson.id'],
            #['tblFlag_id'], [u'tblFlag.id'],
        #),
        sa.PrimaryKeyConstraint(u'tblPerson_id', u'tblFlag_id'), {}
    )

class tblFlag(Base):
    __tablename__ = 'tblFlag'
    id = sa.Column( mysql.INTEGER(display_width=11), nullable=False)
    name = sa.Column( mysql.VARCHAR(length=124), nullable=False)
flag = sa.Column( mysql.TINYINT(display_width=1), server_default='0', autoincrement=False, nullable=False) description = sa.Column( mysql.VARCHAR(length=245), server_default='', nullable=True)
    __table_args__ = (
        sa.PrimaryKeyConstraint(u'id'), {}
    )
    flagged_persons = relationship(
        'tblPerson',
        secondary=tblPerson_has_Flag.__table__,
        backref="flags",
        #collection_class=attribute_mapped_collection('id')
    )

--
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 http://groups.google.com/group/sqlalchemy?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to