Running manage.py makemigrations for multiple databases in django - django-models

I'm trying to use multiple databases in my Django project i.e. MongoDB and MySQL.
# settings.py
DATABASES = {
'default': {
'NAME': 'sql_db',
'ENGINE': 'django.db.backends.mysql',
'USER': 'root',
'PASSWORD': 'root'
},
'mongoproject': {
'ENGINE' : 'djongo',
'NAME': 'mongo_db'
}
}
I create my model for eg
# App/models.py
class ForSQL(models.Model):
Name = models.CharField(max_length=50)
City = models.CharField(max_length=50)
I want to save it in MySQL.
For that, I will run manage.py makemigrations and then manage.py migrate. It will save in MySQL.
But when I add one more table in models.py file like:
# App/models.py
class Mongo(models.Model):
Name = models.CharField(max_length=50)
City = models.CharField(max_length=50)
I want to save this table in MongoDB.
If I run manage.py makemigrations and then run manage.py migrate --database=mongoproject, it saves 'Mongo' Table in MongoDB and also saves 'ForSQL' table in MongoDB because of previous migrations.
I need help in setting routers for differentiating migrations on each table.
I looked for this solution but can't get them working.

Here's how I solved my problem
# routers.py
import App.models
allmodels = dict([(name.lower(), cls) for name, cls in App.models.__dict__.items() if isinstance(cls, type)])
def allow_migrate(self, db, app_label, model_name = None, **hints):
""" migrate to appropriate database per model """
try:
model = allmodels.get(model_name)
return(model.params.db == db)
except:
pass
# App/models.py
class ForSQL(models.Model):
class params:
db = 'default'
Name = models.CharField(max_length=50)
City = models.CharField(max_length=50)
class Mongo(models.Model):
class params:
db = 'mongoproject'
Name = models.CharField(max_length=50)
City = models.CharField(max_length=50)
Then simply run commands manage.py makemigrations and manage.py migrate --database=default for default database (in my case MySQL) and manage.py migrate --database=mongoproject for MongoDB.
It will save ForSQL table in MySQL and Mongo table in MongoDB.
This is what I exactly needed.
Hope it will help someone.
Here it is what helped me https://stackoverflow.com/a/60453322/12134251

Related

why my new table cannot be added to my database

I decide to add new Model called Project to my project:
When I run python manage.py migrate, it shows me the below error:
class Project(models.Model):
statut_juridique=[
('per', 'personne physique' ),
('sarl', 'SARL'),
('sual', 'SUARL'),
('anony', 'SA'),
]
type_du_projet = [
('ind', 'industrie'),
('agr', 'agronome'),
('ser', 'service'),
('art', 'artisanat'),
('com', 'commerce'),
]
name = models.CharField(max_length=50)
produit = ArrayField(
ArrayField(
models.CharField(max_length=20, blank=True),
size=8,
),
size=8,
)
stat_jur = models.CharField(max_length=50, choices=statut_juridique)
type_projet = models.CharField(max_length=50, choices=type_du_projet)
Nomination = models.CharField(max_length=50)
adresse = models.CharField(max_length=200)
user = models.ForeignKey(User, related_name='projet', on_delete=models.CASCADE)
def __str__(self):
return self.name
Operations to perform: Apply all migrations: admin, auth,
businesplan, contenttypes, sessions Running migrations: Applying
contenttypes.0001_initial...Traceback (most recent call last): File
"/home/abdallah/projectdjango/oasis/venv/lib/python3.8/site-packages/django/db/backends/utils.py",
line 87, in _execute
return self.cursor.execute(sql) psycopg2.errors.DuplicateTable: relation "django_content_type" already exists
And also I can't see the new table in my Database, Can you help me please!
You probably use a database that already has some tables with their migrations.
For this case you can Try using a new database, or reset your existing database to remove duplicate tables or sometimes you can troubleshot using --fake-initial as one of django-admin cmd option:
$ python manage.py migrate --fake-initial
From Django-Doc:
Allows Django to skip an app’s initial migration if all database tables with the names of all models created by all CreateModel operations in that migration already exist. This option is intended for use when first running migrations against a database that preexisted the use of migrations. This option does not, however, check for matching database schema beyond matching table names and so is only safe to use if you are confident that your existing schema matches what is recorded in your initial migration.
Source: django-admin#cmdoption-migrate-fake-initial [django-doc]

Duplicate tables created in django migrations when using SQL Server schemas

I want to place all Django specific tables and my custom Auth models into the default dbo schema, and have all my different app specific tables in a schema named after the app. Something to note is that all of my app tables will foreign key back to my auth model (I have _created_by and _last_updated_by fields on a base model that all apps inherit from). Basically I want the DB structure to be something like this:
DBO
- my_custom_auth_table
- django_migrations
- django_session
- django_content_type
- etc...
APP1
-table1
-table2
APP2
-table1
-table2
In order to achieve this, I tried creating a Login/User pair on the DB server for each app and implemented a DB router.
my allow_migrate method:
def allow_migrate(self, db, app_label, model_name=None, **hints):
if app_label == db:
return True
else:
return False
my database settings (I will use my doglicense app as an example):
IP = xxx
default_db_settings = {
'ENGINE': 'mssql',
'NAME': 'DB',
'USER': 'some_user',
'PASSWORD':'***',
'HOST': IP,
'PORT':'1433',
'OPTIONS':{'driver':'ODBC Driver 18 for SQL Server', 'extra_params': 'trustServerCertificate=yes'},
}
doglicense = {
'ENGINE': 'mssql',
'NAME': 'DB',
'USER': 'DogLicense',
'PASSWORD':'***',
'HOST': IP,
'PORT':'1433',
'OPTIONS':{'driver':'ODBC Driver 18 for SQL Server', 'extra_params': 'trustServerCertificate=yes'},
}
I have successfully migrated the custom auth app and all of djangos apps into dbo, however this is where the fun begins.
If I run:
python manage.py migrate DogLicense --plan
we can see that it only tries to create the new tables:
Planned operations:
DogLicense.0001_initial
Create model Breed
Create model Color
Create model Dog
Create model ZipCode
Create model Veterinarian
Create model Street
Create model Registration
Create model Owner
Create model DogType
Add field owners to dog
Add field type to dog
However when I try to specify the database connection in order to dump theses files into the doglicense schema:
python manage.py migrate DogLicense --plan --database=doglicense
I get:
Planned operations:
contenttypes.0001_initial
Create model ContentType
Alter unique_together for contenttype (1 constraint(s))
contenttypes.0002_remove_content_type_name
Change Meta options on contenttype
Alter field name on contenttype
Raw Python operation
Remove field name from contenttype
auth.0001_initial
Create model Permission
Create model Group
Create model User
auth.0002_alter_permission_name_max_length
Alter field name on permission
auth.0003_alter_user_email_max_length
Alter field email on user
auth.0004_alter_user_username_opts
Alter field username on user
auth.0005_alter_user_last_login_null
Alter field last_login on user
auth.0006_require_contenttypes_0002
auth.0007_alter_validators_add_error_messages
Alter field username on user
auth.0008_alter_user_username_max_length
Alter field username on user
auth.0009_alter_user_last_name_max_length
Alter field last_name on user
auth.0010_alter_group_name_max_length
Alter field name on group
auth.0011_update_proxy_permissions
Raw Python operation -> Update the content_type of prox…
auth.0012_alter_user_first_name_max_length
Alter field first_name on user
MSSAuth.0001_initial
Create model FailedLoginAttempt
Create model MSSUser
Create model AuthProfile
DogLicense.0001_initial
Create model Breed
Create model Color
Create model Dog
Create model ZipCode
Create model Veterinarian
Create model Street
Create model Registration
Create model Owner
Create model DogType
Add field owners to dog
Add field type to dog
You can see it wants to create every table all over again. And obviously running this migration without the --plan flag does indeed result in tables like doglicense.django_migrations being created.
How can I prevent these duplicate tables from being created? Is this a problem with my SQL Server user permissions? Perhaps my router is poorly implemented?
Any help will be appreciated.

I deleted my migration file in django and i migrated it but it does not migrate

After deleting my migration file, I coded the table messages, but no response
this is my table:
class Person(models.Model):
id = models.AutoField(primary_key=True)
first_name = models.CharField(max_length=30)
last_name = models.CharField(max_length=30)
class Message(models.Model):
user_code = models.ForeignKey(Person, on_delete=models.CASCADE)
messages = models.TextField(max_length=5000)
and it says:
./python manage.py migrate
Operations to perform: y migrate
Apply all migrations: admin, auth, contenttypes, sessions
Running migrations:
No migrations to apply.
And in error it displays:
OperationalError at /admin/Login/message/
I have also tried clearing the table...

whoosh search after database migration

I migrated my database from sqlite3 to postgresql with the following three steps
I create a sqlite dump file with
sqlite3 app.db .dump > app_dump.sql
I create a postgresql database and initialized it with my models.py file as
python manage.py db init
where manage.py is
SQLALCHEMY_DATABASE_URI = 'postgresql+psycopg2://appuser:777#localhost/app_db'
migrate = Migrate(app, db)
manager = Manager(app)
manager.add_command('db', MigrateCommand)
if __name__ == '__main__':
manager.run()
I used only the insert lines in the dump file to populate the new database.
This worked well, however I do want to have a whoosh search in my User table.
My models.py looks like this
if sys.version_info >= (3, 0):
enable_search = False
else:
enable_search = True
import flask.ext.whooshalchemy as whooshalchemy
class User(db.Model):
__searchable__ = ['username','id']
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(100), index=True)
if enable_search:
whooshalchemy.whoosh_index(app, User)
However, the search does not seem to work at all. I fear that the table has not been properly indexed? Anybody know how I can fix this?
thanks
carl
EDIT:
Solved it... have a look at
https://gist.github.com/davb5/21fbffd7a7990f5e066c
By default flask_whooshalchemy indexes a record when the sqlalchemy session commits. So if you want to index all non-indexed-data you can try my fork which is called flask_whooshalchemyplus see the manually indexing introduction.

Django makemigration error in only a table

I have a django 1.8 project and I use so many models. I decide to create a new column in an specific model but it doesn't work because "the column 'x' doesn't exists". When I try the same in other model it works fine, is just in that model. I could drop that model and create it again, but I need to fix it without erase anything. Any idea?
column = models.CharField(max_length=50, null=True)
python3 manage.py makemigrations
... django.db.utils.ProgrammingError: column table.column doesn't exists
Finally I could update my model by editing the migration.py file like:
class Migration(migrations.Migration):
dependencies = [
('appName', '000X_auto_...'),
]
operations = [
migrations.AddField(
model_name='modelName',
name='column',
field=models.CharField(max_length=50, null=True),
),

Resources