Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fileField saved in default db even after switch_db() #1301

Open
adityaContributes opened this issue May 19, 2016 · 2 comments
Open

fileField saved in default db even after switch_db() #1301

adityaContributes opened this issue May 19, 2016 · 2 comments

Comments

@adityaContributes
Copy link

files are getting stored in default db even with using switch_db()

Please refer the below code that demostrates the case.
Mongo engine VERSION = 0.10.6

#models.py
from mongoengine import FileField, Document, StringField 
from mongoengine import connect, register_connection

connect('ets', alias='default')
register_connection('ets_archive','ets_archive')

class Mediafile(Document):
    content_type = StringField(max_length=255, required=True)
    file_name = StringField(max_length=1024, required=True)
    file_member = FileField( default=None)
from models import Mediafile
from mongoengine.context_managers import switch_db

def save_db():
    with switch_db(Mediafile, 'ets_archive') as Mediafile_export_db:
        mediafile = Mediafile_export_db()
        mediafile.content_type = 'text/plain'
        mediafile.file_name = 'new_db.txt'
        mediafile.file_member.new_file()
        mediafile.file_member.write('new db'.encode("UTF-8"))
        mediafile.file_member.close()
        mediafile.save()

if __name__ == '__main__':
    save_db()

When I run test.py the file is stored to the default database.
Note there Mediafile Document is still stored in the new archive database. Its only the (gridfs)file thats stored in the default database instead of database named 'ets_archive'

@lafrech lafrech added the Bug label May 20, 2016
@lafrech lafrech added this to the Scheduled, not planned milestone May 20, 2016
@amcgregor
Copy link
Contributor

I can not fully express the impact of this bug on my codebases at work. Isolated-database application multi-tenancy is basically impossible if GridFS file storage is utilized through MongoEngine, and it was a hell of a shocker to find files that must be stored separately instead pooled together in the default connection.

@nadavsn
Copy link

nadavsn commented Dec 9, 2017

See #1365 for a workaround.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants