Hello, I was wondering if anyone knows a good way to handle restoring a backup taken with a m2m intermediary model.
I'm trying to upgrade from my sqlite database to a real database and would like to keep the data that was created thus far.
Basically the models look like this:
class Message(models.Model):
subject = models.CharField(max_length=64)
...
recipients = models.ManyToManyField(User, through='SentRecord', blank=True)
class SentRecord(models.Model):
message = models.ForeignKey(Message)
recipient = models.ForeignKey(User)
sent_at = models.DateTimeField(auto_now=True)
So I've tried both dumpdata/loaddata and dumpscript/runscript. The former basically quietly ignores the records but everything else is OK -- but no Messages or SentRecords.
So I've tried two approaches -- first problem was regarding the fact that SentRecord and Message refer to one another and so one has to be created first. dumpscript picked SentRecord to do first -- totally sensible. But It can't comply with the above model because the .message foreign key will create a column that enforces NOT NULL.
OK fine, I changed the above model to allow null on message, since it basically does this sequence:
1. create sent messages, with no message pk
2. create messages
3. then it *should* fill in the sent messages .message pk... but instead it blows up because because it generates stuff like this:
app_message_1.recipient.add( importer.locate_object(User, 'id', 1, ...
Which is a big no no because of the through record it's using wrong manager. It should be getting the sent records already created and setting the .message pk on those instead of trying to add Users directly to the message, as though it were a regular m2m.