I don’t see a primary key on your monthlyusage table, which is where the issue is occurring, so it depends on which col is the PK. typically the pk would be a composite of the company_id and domain_id columns.
On Oct 31, 2013, at 10:20 PM, thinkwelldesi...@gmail.com wrote: > > 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. -- 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.