<http://stackoverflow.com/questions/19716518/sqlalchemy-many-to-many-table-with-multiple-foreign-key-entires#>
 
  
I'm new with sqlalchemy and I want to do this as simply as possible, yet 
correctly. I want to track domain use across multiple companies on a 
monthly basis, so I set up the following tables:


class Company(Base):
    __tablename__ = 'company'

    id = Column(Integer, primary_key = True)
    name = Column('name', String)

class Domains(Base):
    __tablename__ = 'domains'

    id = Column(Integer, primary_key=True)
    name = Column('name', String, unique=True)
class MonthlyUsage(Base):
    '''
    Track domain usage across all
    companies on a monthly basis.
    '''
    __tablename__ = 'monthlyusage'

    month = Column(DateTime)
    company_id = Column(Integer, ForeignKey('company.id'))
    domain_id  = Column(Integer, ForeignKey('domains.id'))

    # <...other columns snipped out...>

    company = relationship('Company', backref='company_assoc')
    domain = relationship('Domains', backref='domain_assoc')


This works fine, until I add usage details for the second month. Then I get 
duplicate key value errors:

*sqlalchemy.exc.IntegrityError: (IntegrityError) duplicate key value 
violates unique constraint "monthlyusage_pkey"*

Does this mean I have to split out the "monthlyusage" into another m2m 
table? That seems unnecessarily complicated, since all that needs to be 
unique is the month, company_id, and domain_id fields.

Any suggestions for my layout here, to keep it as simple as possible, yet 
still correct?

Thanks!


-- 
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.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to