samedi 27 février 2016

Django Moving an object from one database to another

I am trying to migrate from SQLite to postgreSQL after trying Django builtin command dumpdata here

Found another way in the official docs, The data is migrated without any error but when I am trying to save instance of any Model via shell or form I am getting pk already exist error. Somehow django isn't aware of existing values . . . .

for firm in models.Firm.objects.using("olddb").all():
    firm.save(using="default")

Now default database has all the objects from the olddb

>>> Firm.objects.all()[0]
<Firm: ABC>
>>> Firm.objects.all()[0].id
1
>>> f = Firm()
>>> f.name = "ABDEGVHG"
>>> f.save(using="default")
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\Users\vaibh\envs\py3\lib\site-packages\django\db\models\base.py", line 590, in save
    force_update=force_update, update_fields=update_fields)
  File "C:\Users\vaibh\envs\py3\lib\site-packages\django\db\models\base.py", line 618, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "C:\Users\vaibh\envs\py3\lib\site-packages\django\db\models\base.py", line 699, in _save_table
    result = self._do_insert(cls._base_manager, using, fields, update_pk, raw)
  File "C:\Users\vaibh\envs\py3\lib\site-packages\django\db\models\base.py", line 732, in _do_insert
    using=using, raw=raw)
  File "C:\Users\vaibh\envs\py3\lib\site-packages\django\db\models\manager.py", line 92, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "C:\Users\vaibh\envs\py3\lib\site-packages\django\db\models\query.py", line 921, in _insert
    return query.get_compiler(using=using).execute_sql(return_id)
  File "C:\Users\vaibh\envs\py3\lib\site-packages\django\db\models\sql\compiler.py", line 920, in execute_sql
    cursor.execute(sql, params)
  File "C:\Users\vaibh\envs\py3\lib\site-packages\django\db\backends\utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "C:\Users\vaibh\envs\py3\lib\site-packages\django\db\utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "C:\Users\vaibh\envs\py3\lib\site-packages\django\utils\six.py", line 549, in reraise
    raise value.with_traceback(tb)
  File "C:\Users\vaibh\envs\py3\lib\site-packages\django\db\backends\utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
django.db.utils.IntegrityError: duplicate key value violates unique constraint "girvi_firm_pkey"
DETAIL:  Key (id)=(1) already exists.

Can someone please tell me how to resolve this.

Aucun commentaire:

Enregistrer un commentaire