IntegrityError
The issue is that the samples are instantiated before the `Group` has an ID. Peewee looks at the group passed in (`group=self`) and attempts to read an ID off it. Since it has no ID at the time of assignment, you get the error you're seeing. Put the call to `g.save()` before `g.samples()[0].name` and you'll be set.
 Thank you for your answer.
Is it possible to make the samples reread the group id?
I want to assign properties to the samples before saving the group itself.
(I think I could work around that by using a transaction, but unfortunately the corresponding code is quite distributed.)
 It's just python so you can do whatever you want!

``` python
    def samples(self):
        """Samples are generated the first time they are requested"""
        if self._samples is None:
            self._samples = [Sample() for _ in range(10)]
        for sample in self._samples:
            sample.group = self
        return self._samples
```
  Thanks! :+1:
Didn't think of that.

Example code:

``` python
from peewee import *

database = SqliteDatabase(':memory:', threadlocals=True)


class BaseModel(Model):
    class Meta:
        database = database


class Group(BaseModel):
    name = IntegerField()
    _samples = None

    def samples(self):
        """Samples are generated the first time they are requested"""
        if self._samples is None:
            self._samples = [Sample(group=self) for _ in range(10)]
        return self._samples

    def __repr__(self):
        return '<Group(name={})>'.format(self.name)


class Sample(BaseModel):
    name = CharField(null=True)
    group = ForeignKeyField(Group)

    def __repr__(self):
        return '<Sample(name="{}", group={})>'.format(self.name, self.group)


Group.create_table()
Sample.create_table()


# This works as expected
# ----------------------
g = Group(name=1)
g.save()
g.samples()[0].name = 'Something'

for sample in g.samples():
    sample.save()


# This doesn't work although the group should have an ID when saved?
# ------------------------------------------------------------------
g = Group(name=2)
g.samples()[0].name = 'Something else'
g.save()

for sample in g.samples():
    sample.save()
```

Maybe I'm doing that wrong, but shouldn't the group have an ID as soon as it is saved?
