0

factory_boy を使用して、作業中のアプリのファクトリを作成しています。別のモデルと 1 対 1 の関係を持つモデルのファクトリを作成しようとすると、問題が発生します。

モデルは次のとおりです。

class Playlist(AccountDependantMixin, models.Model):
    test = models.OneToOneField('core.PlaylistTest', related_name='playlist')

class PlaylistTest(Test):
    pass

AccountDependantMixin は、追加情報を含むクラスです。他のモデルも必要なので外です。さまざまな種類のテストがあります。そのため、 PlaylistTest は空です

これは工場です:

class PlaylistTestFactory(factory.DjangoModelFactory):
    class Meta:
        model = PlaylistTest


class PlaylistFactory(factory.DjangoModelFactory):
    class Meta:
        model = Playlist       
    test = factory.SubFactory(PlaylistTestFactory)

そして、工場でインスタンスを初期化しようとしている方法は次のとおりです。

self.playlist = PlaylistFactory(creator=AdminUserFactory(account=self.account))

次のエラーが表示されます。

IntegrityError: null value in column "test_id" violates not-null constraint
DETAIL:  Failing row contains (1, , playlist0, sub_title0, description0, 0, t, f, 2016-03-31 12:49:23.739207+00, 0, 2, 1, null)
4

2 に答える 2

1

test = factory.RelatedFactory(PlaylistTestFactory)

最初にテスト オブジェクトを作成するには、 aSubFactoryではなく aを使用する必要があります。RelatedFactory

RelatedFactory は、主に SubFactory のように動作しますが、関連する Factory がベース Factory の後に生成されるという主な違いがあります。

https://factoryboy.readthedocs.org/en/latest/reference.html#factory.RelatedFactory

于 2016-03-31T15:18:39.240 に答える