FactoryBoy / factory_boy

A test fixtures replacement for Python
https://factoryboy.readthedocs.io/
MIT License
3.52k stars 398 forks source link

Django: Column specified more than once #156

Closed jonathan-s closed 6 years ago

jonathan-s commented 10 years ago

I have the following model setup in Django

class VotingBase(models.Model):
  hangar_id = models.IntegerField()
  # fields here
  class Meta:
    abstract = True

class Voting(VotingBase):
  document = models.ForeignKey(
        'Document', to_field='hangar_id', db_column='hangar_id', related_name='doc_votes') 

class VotingDistinct(VotingBase):
  document = models.ForeignKey(
        'Document', to_field='hangar_id', db_column='hangar_id', related_name='ddoc_votes')

  class Meta:
    db_table = 'table_here'
    managed = False

# from factories.py 
class VotingFactory(factory.django.DjangoModelFactory):

  class Meta:
    model = Voting

  document = factory.SubFactory(DocumentFactory)
  # other fields here. 

The error I get when creating a VotingFactory is the cryptic column "hangar_id" specified more than once. Any ideas for this?

jonathan-s commented 10 years ago

Ok, I have better explanation to what's going on here. There is a conflict with hangar_id in the model and the foreign key document. FactoryBoy creates two hangar_id, even though it should not create another because of the syntax in the foreign key that says it should use the already existing hangar_id

It would be neat if this could be fixed :)

rbarrois commented 10 years ago

@jonathan-s Could you paste the output of this:

with factory.debug():
    DocumentFactory()
    VotingFactory()

Thanks!

jonathan-s commented 10 years ago

A note, you'll see VotingAggFactory below, it's the same issue as VotingFactory and set up in the same way. Here you go:

BaseFactory: Preparing riksdagen.tests.factories.DocumentFactory(extra={}) BaseFactory: Preparing riksdagen.tests.factories.DocumentFactory(extra={}) <class 'riksdagen.tests.factories.DocumentFactory'>: Setting up next sequence (1) <class 'riksdagen.tests.factories.DocumentFactory'>: Setting up next sequence (1) LazyStub: Computing values for riksdagen.tests.factories.DocumentFactory(status='', documentstatus_url_xml='http://data.riksdagen.se/dokumentstatus/GY01AU4', doctype2='bet', doctype='bet', party_year='2013/14', committee_prop_url_xml='http://data.riksdagen.se/utskottsforslag/GY01AU4', document_url_text='http://data.riksdagen.se/dokument/GY01AU4/text', serial_num_end=0, subtitle='', hangar_id=<OrderedDeclarationWrapper for <factory.declarations.Sequence object at 0x10baea710>>, sourceid='', title='Random Junk Title', htmlformat='', templabel='', publicised=datetime.datetime(2014, 6, 22, 0, 0), govorgan='FIU', system_date=datetime.datetime(2014, 6, 22, 0, 0), document_url_html='Lots of html', date=datetime.date(2014, 6, 22), subtype='bet', doc_id='GY01AU4', label='FIU21', html='Again lots of html', serial_num=4, related_id='', receiver='', source='') LazyStub: Computing values for riksdagen.tests.factories.DocumentFactory(status='', documentstatus_url_xml='http://data.riksdagen.se/dokumentstatus/GY01AU4', doctype2='bet', doctype='bet', party_year='2013/14', committee_prop_url_xml='http://data.riksdagen.se/utskottsforslag/GY01AU4', document_url_text='http://data.riksdagen.se/dokument/GY01AU4/text', serial_num_end=0, subtitle='', hangar_id=<OrderedDeclarationWrapper for <factory.declarations.Sequence object at 0x10baea710>>, sourceid='', title='Random Junk Title', htmlformat='', templabel='', publicised=datetime.datetime(2014, 6, 22, 0, 0), govorgan='FIU', system_date=datetime.datetime(2014, 6, 22, 0, 0), document_url_html='Lots of html', date=datetime.date(2014, 6, 22), subtype='bet', doc_id='GY01AU4', label='FIU21', html='Again lots of html', serial_num=4, related_id='', receiver='', source='') Sequence: Computing next value of <function DocumentFactory. at 0x10bada710> for seq=1 Sequence: Computing next value of <function DocumentFactory. at 0x10bada710> for seq=1 LazyStub: Computed values, got riksdagen.tests.factories.DocumentFactory(status='', documentstatus_url_xml='http://data.riksdagen.se/dokumentstatus/GY01AU4', doctype='bet', party_year='2013/14', committee_prop_url_xml='http://data.riksdagen.se/utskottsforslag/GY01AU4', document_url_text='http://data.riksdagen.se/dokument/GY01AU4/text', system_date=datetime.datetime(2014, 6, 22, 0, 0), subtitle='', hangar_id=2001, sourceid='', title='Random Junk Title', htmlformat='', templabel='', publicised=datetime.datetime(2014, 6, 22, 0, 0), govorgan='FIU', serial_num_end=0, document_url_html='Lots of html', date=datetime.date(2014, 6, 22), subtype='bet', doc_id='GY01AU4', label='FIU21', html='Again lots of html', doctype2='bet', related_id='', source='', receiver='', serial_num=4) LazyStub: Computed values, got riksdagen.tests.factories.DocumentFactory(status='', documentstatus_url_xml='http://data.riksdagen.se/dokumentstatus/GY01AU4', doctype='bet', party_year='2013/14', committee_prop_url_xml='http://data.riksdagen.se/utskottsforslag/GY01AU4', document_url_text='http://data.riksdagen.se/dokument/GY01AU4/text', system_date=datetime.datetime(2014, 6, 22, 0, 0), subtitle='', hangar_id=2001, sourceid='', title='Random Junk Title', htmlformat='', templabel='', publicised=datetime.datetime(2014, 6, 22, 0, 0), govorgan='FIU', serial_num_end=0, document_url_html='Lots of html', date=datetime.date(2014, 6, 22), subtype='bet', doc_id='GY01AU4', label='FIU21', html='Again lots of html', doctype2='bet', related_id='', source='', receiver='', serial_num=4) BaseFactory: Generating riksdagen.tests.factories.DocumentFactory(status='', documentstatus_url_xml='http://data.riksdagen.se/dokumentstatus/GY01AU4', doctype2='bet', doctype='bet', party_year='2013/14', committee_prop_url_xml='http://data.riksdagen.se/utskottsforslag/GY01AU4', document_url_text='http://data.riksdagen.se/dokument/GY01AU4/text', system_date=datetime.datetime(2014, 6, 22, 0, 0), subtitle='', hangar_id=2001, sourceid='', title='Random Junk Title', htmlformat='', templabel='', publicised=datetime.datetime(2014, 6, 22, 0, 0), govorgan='FIU', serial_num_end=0, document_url_html='Lots of html', date=datetime.date(2014, 6, 22), subtype='bet', doc_id='GY01AU4', label='FIU21', html='Again lots of html', serial_num=4, related_id='', receiver='', source='') BaseFactory: Generating riksdagen.tests.factories.DocumentFactory(status='', documentstatus_url_xml='http://data.riksdagen.se/dokumentstatus/GY01AU4', doctype2='bet', doctype='bet', party_year='2013/14', committee_prop_url_xml='http://data.riksdagen.se/utskottsforslag/GY01AU4', document_url_text='http://data.riksdagen.se/dokument/GY01AU4/text', system_date=datetime.datetime(2014, 6, 22, 0, 0), subtitle='', hangar_id=2001, sourceid='', title='Random Junk Title', htmlformat='', templabel='', publicised=datetime.datetime(2014, 6, 22, 0, 0), govorgan='FIU', serial_num_end=0, document_url_html='Lots of html', date=datetime.date(2014, 6, 22), subtype='bet', doc_id='GY01AU4', label='FIU21', html='Again lots of html', serial_num=4, related_id='', receiver='', source='') /Users/jonathan/.virtualenvs/riksdagsrosten/lib/python3.3/site-packages/django/db/models/fields/init.py:903: RuntimeWarning: DateTimeField Document.system_date received a naive datetime (2014-06-22 00:00:00) while time zone support is active. RuntimeWarning)

/Users/jonathan/.virtualenvs/riksdagsrosten/lib/python3.3/site-packages/django/db/models/fields/init.py:903: RuntimeWarning: DateTimeField Document.publicised received a naive datetime (2014-06-22 00:00:00) while time zone support is active. RuntimeWarning)

BaseFactory: Preparing riksdagen.tests.factories.VotingAggFactory(extra={}) BaseFactory: Preparing riksdagen.tests.factories.VotingAggFactory(extra={}) <class 'riksdagen.tests.factories.VotingAggFactory'>: Setting up next sequence (1) <class 'riksdagen.tests.factories.VotingAggFactory'>: Setting up next sequence (1) LazyStub: Computing values for riksdagen.tests.factories.VotingAggFactory(document=<OrderedDeclarationWrapper for <factory.declarations.SubFactory object at 0x10baea790>>, voting_id='AAFAC7F5-AFCD-11D8-AE5D-0004755038D1', q1_absent=0, q1_abstained=0, q1_no=0, hangar_id=<OrderedDeclarationWrapper for <factory.declarations.Sequence object at 0x10baea690>>, date=datetime.date(2014, 6, 17), q1_yes=0) LazyStub: Computing values for riksdagen.tests.factories.VotingAggFactory(document=<OrderedDeclarationWrapper for <factory.declarations.SubFactory object at 0x10baea790>>, voting_id='AAFAC7F5-AFCD-11D8-AE5D-0004755038D1', q1_absent=0, q1_abstained=0, q1_no=0, hangar_id=<OrderedDeclarationWrapper for <factory.declarations.Sequence object at 0x10baea690>>, date=datetime.date(2014, 6, 17), q1_yes=0) SubFactory: Instantiating riksdagen.tests.factories.DocumentFactory(containers=(,)), create=True SubFactory: Instantiating riksdagen.tests.factories.DocumentFactory(containers=(,)), create=True BaseFactory: Preparing riksdagen.tests.factories.DocumentFactory(extra={'containers': (,)}) BaseFactory: Preparing riksdagen.tests.factories.DocumentFactory(extra={'containers': (,)}) LazyStub: Computing values for riksdagen.tests.factories.DocumentFactory(status='', documentstatus_url_xml='http://data.riksdagen.se/dokumentstatus/GY01AU4', doctype2='bet', doctype='bet', party_year='2013/14', committee_prop_url_xml='http://data.riksdagen.se/utskottsforslag/GY01AU4', document_url_text='http://data.riksdagen.se/dokument/GY01AU4/text', serial_num_end=0, subtitle='', hangar_id=<OrderedDeclarationWrapper for <factory.declarations.Sequence object at 0x10baea710>>, sourceid='', title='Random Junk Title', htmlformat='', templabel='', publicised=datetime.datetime(2014, 6, 22, 0, 0), govorgan='FIU', system_date=datetime.datetime(2014, 6, 22, 0, 0), document_url_html='Lots of html', date=datetime.date(2014, 6, 22), subtype='bet', doc_id='GY01AU4', label='FIU21', html='Again lots of html', serial_num=4, related_id='', receiver='', source='') LazyStub: Computing values for riksdagen.tests.factories.DocumentFactory(status='', documentstatus_url_xml='http://data.riksdagen.se/dokumentstatus/GY01AU4', doctype2='bet', doctype='bet', party_year='2013/14', committee_prop_url_xml='http://data.riksdagen.se/utskottsforslag/GY01AU4', document_url_text='http://data.riksdagen.se/dokument/GY01AU4/text', serial_num_end=0, subtitle='', hangar_id=<OrderedDeclarationWrapper for <factory.declarations.Sequence object at 0x10baea710>>, sourceid='', title='Random Junk Title', htmlformat='', templabel='', publicised=datetime.datetime(2014, 6, 22, 0, 0), govorgan='FIU', system_date=datetime.datetime(2014, 6, 22, 0, 0), document_url_html='Lots of html', date=datetime.date(2014, 6, 22), subtype='bet', doc_id='GY01AU4', label='FIU21', html='Again lots of html', serial_num=4, related_id='', receiver='', source='') Sequence: Computing next value of <function DocumentFactory. at 0x10bada710> for seq=2 Sequence: Computing next value of <function DocumentFactory. at 0x10bada710> for seq=2 LazyStub: Computed values, got riksdagen.tests.factories.DocumentFactory(status='', documentstatus_url_xml='http://data.riksdagen.se/dokumentstatus/GY01AU4', doctype='bet', party_year='2013/14', committee_prop_url_xml='http://data.riksdagen.se/utskottsforslag/GY01AU4', document_url_text='http://data.riksdagen.se/dokument/GY01AU4/text', system_date=datetime.datetime(2014, 6, 22, 0, 0), subtitle='', hangar_id=2002, sourceid='', title='Random Junk Title', htmlformat='', templabel='', publicised=datetime.datetime(2014, 6, 22, 0, 0), govorgan='FIU', serial_num_end=0, document_url_html='Lots of html', date=datetime.date(2014, 6, 22), subtype='bet', doc_id='GY01AU4', label='FIU21', html='Again lots of html', doctype2='bet', related_id='', source='', receiver='', serial_num=4) LazyStub: Computed values, got riksdagen.tests.factories.DocumentFactory(status='', documentstatus_url_xml='http://data.riksdagen.se/dokumentstatus/GY01AU4', doctype='bet', party_year='2013/14', committee_prop_url_xml='http://data.riksdagen.se/utskottsforslag/GY01AU4', document_url_text='http://data.riksdagen.se/dokument/GY01AU4/text', system_date=datetime.datetime(2014, 6, 22, 0, 0), subtitle='', hangar_id=2002, sourceid='', title='Random Junk Title', htmlformat='', templabel='', publicised=datetime.datetime(2014, 6, 22, 0, 0), govorgan='FIU', serial_num_end=0, document_url_html='Lots of html', date=datetime.date(2014, 6, 22), subtype='bet', doc_id='GY01AU4', label='FIU21', html='Again lots of html', doctype2='bet', related_id='', source='', receiver='', serial_num=4) BaseFactory: Generating riksdagen.tests.factories.DocumentFactory(status='', documentstatus_url_xml='http://data.riksdagen.se/dokumentstatus/GY01AU4', doctype2='bet', doctype='bet', party_year='2013/14', committee_prop_url_xml='http://data.riksdagen.se/utskottsforslag/GY01AU4', document_url_text='http://data.riksdagen.se/dokument/GY01AU4/text', system_date=datetime.datetime(2014, 6, 22, 0, 0), subtitle='', hangar_id=2002, sourceid='', title='Random Junk Title', htmlformat='', templabel='', publicised=datetime.datetime(2014, 6, 22, 0, 0), govorgan='FIU', serial_num_end=0, document_url_html='Lots of html', date=datetime.date(2014, 6, 22), subtype='bet', doc_id='GY01AU4', label='FIU21', html='Again lots of html', serial_num=4, related_id='', receiver='', source='') BaseFactory: Generating riksdagen.tests.factories.DocumentFactory(status='', documentstatus_url_xml='http://data.riksdagen.se/dokumentstatus/GY01AU4', doctype2='bet', doctype='bet', party_year='2013/14', committee_prop_url_xml='http://data.riksdagen.se/utskottsforslag/GY01AU4', document_url_text='http://data.riksdagen.se/dokument/GY01AU4/text', system_date=datetime.datetime(2014, 6, 22, 0, 0), subtitle='', hangar_id=2002, sourceid='', title='Random Junk Title', htmlformat='', templabel='', publicised=datetime.datetime(2014, 6, 22, 0, 0), govorgan='FIU', serial_num_end=0, document_url_html='Lots of html', date=datetime.date(2014, 6, 22), subtype='bet', doc_id='GY01AU4', label='FIU21', html='Again lots of html', serial_num=4, related_id='', receiver='', source='') Sequence: Computing next value of <function VotingAggFactory. at 0x10bada050> for seq=1 Sequence: Computing next value of <function VotingAggFactory. at 0x10bada050> for seq=1 LazyStub: Computed values, got riksdagen.tests.factories.VotingAggFactory(document=<Document: 2013/14:FIU21 :Random Junk Title>, voting_id='AAFAC7F5-AFCD-11D8-AE5D-0004755038D1', q1_absent=0, q1_abstained=0, q1_no=0, hangar_id=1001, date=datetime.date(2014, 6, 17), q1_yes=0) LazyStub: Computed values, got riksdagen.tests.factories.VotingAggFactory(document=<Document: 2013/14:FIU21 :Random Junk Title>, voting_id='AAFAC7F5-AFCD-11D8-AE5D-0004755038D1', q1_absent=0, q1_abstained=0, q1_no=0, hangar_id=1001, date=datetime.date(2014, 6, 17), q1_yes=0) BaseFactory: Generating riksdagen.tests.factories.VotingAggFactory(document=<Document: 2013/14:FIU21 :Random Junk Title>, voting_id='AAFAC7F5-AFCD-11D8-AE5D-0004755038D1', q1_absent=0, q1_abstained=0, q1_no=0, hangar_id=1001, date=datetime.date(2014, 6, 17), q1_yes=0) BaseFactory: Generating riksdagen.tests.factories.VotingAggFactory(document=<Document: 2013/14:FIU21 :Random Junk Title>, voting_id='AAFAC7F5-AFCD-11D8-AE5D-0004755038D1', q1_absent=0, q1_abstained=0, q1_no=0, hangar_id=1001, date=datetime.date(2014, 6, 17), q1_yes=0)

rbarrois commented 10 years ago

Hmm, this doesn't seem to help :/ I think I'll need the description of the Document model and the related DocumentFactory...

jonathan-s commented 6 years ago

Closing this issue as I won't be able to provide info any longer...