K
K
Konstantin Malyarov2019-12-03 15:43:58
Django
Konstantin Malyarov, 2019-12-03 15:43:58

How to do migrations in Django + MongoDB?

There is a model:

class User(AbstractUser):
    user_id = models.IntegerField(unique=True)
    appeal = models.TextField()
    timestamp = models.IntegerField()
    status = models.IntegerField()
    ref = models.IntegerField()
    btc_wallet = models.TextField()
    eth_wallet = models.TextField()
    start_sub = models.IntegerField()
    end_sub = models.IntegerField()
    lang = models.IntegerField()
    block = models.BooleanField(default=False)
    test_count = models.IntegerField()
    confirmation_code = models.CharField(max_length=10)
    email = models.EmailField()
    password = models.TextField()
    btc_wallet_i = models.TextField()

    def __str__(self):
        return '{}'.format(self.user_id)

There is a second DB which gets over a cycle.
import pymongo

conn = pymongo.MongoClient("localhost", 27017)
db = conn["crypto_signals"]
users_old_db = db["users"]
db = conn["magicsignals"]
users_new_db = db["user_user"]

users = users_old_db.find()

for user in users:
    users_new_db.insert_one(user)

An error is thrown

[email protected]:/var/www/html# python3 copy_db.py
Traceback (most recent call last):
File "copy_db.py", line 12, in
users_new_db.insert_one(user)
File "/usr/local/lib/python3 .6/dist-packages/pymongo/collection.py", line 693, in insert_one
session=session),
File "/usr/local/lib/python3.6/dist-packages/pymongo/collection.py", line 607 , in _insert
bypass_doc_val, session)
File "/usr/local/lib/python3.6/dist-packages/pymongo/collection.py", line 595, in _insert_one
acknowledged, _insert_command, session)
File "/usr/local/lib /python3.6/dist-packages/pymongo/mongo_client.py", line 1248, in _retryable_write
return self._retry_with_session(retryable, func, s, None)
File "/usr/local/lib/python3.6/dist-packages/pymongo/mongo_client.py", line 1201, in _retry_with_session
return func(session, sock_info, retryable)
File "/usr/local/lib/python3.6 /dist-packages/pymongo/collection.py", line 592, in _insert_command
_check_write_command_response(result)
File "/usr/local/lib/python3.6/dist-packages/pymongo/helpers.py", line 217, in _check_write_command_response
_raise_last_write_error(write_errors)
File "/usr/local/lib/python3.6/dist-packages/pymongo/helpers.py", line 198, in _raise_last_write_error
raise DuplicateKeyError(error.get("errmsg"), 11000, error)
pymongo.errors.DuplicateKeyError: E11000 duplicate key error collection: magicsignals.user_user index: __primary_key__ dup key: { : null }

How to get rid of duplication?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Ranwise, 2019-12-04
@Ranwise

Django doesn't support mongo, what kind of migrations are you talking about?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question