Django apps that are meant to be redistributed don't have a manage.py (nor a settings.py since that is part of the project, not the app).
How does one run schemamigration --auto in this scenario? Do I need to have a minimal settings/manage.py as part of the app repository in order to do this? Is there a way to do this with django-admin.py?
You just need to add the app to your Installed_Apps in your settings.py
Then you can run ./manage.py schemamigration <app_name> --auto
If the app doesn't have any migrations you will want to run ./manage.py schemamigration <app_name> --initial first and then ./manage.py schemamigration <app_name> --auto from then on.
Just managed to get this working in one of my project. Here's the code that works for me:
import sys
from django.conf import settings
from django.core.management import call_command
if not settings.configured:
settings.configure(
ROOT_URLCONF='',
DEBUG=False,
DATABASES={
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': 'test.db'
}
},
INSTALLED_APPS=(
'south',
'my_app',
)
)
if __name__ == "__main__":
call_command('schemamigration', 'my_app',
initial=len(sys.argv) > 1,
auto=len(sys.argv) == 0
The above script is saved as migrate.py and run with python migrate.py or python migrate.py i (the i can be anything, and it will use --initial instead of --auto if present). Obviously, you can do fancier command line option parsin, but this works for me.
EDIT: Updated the script, DATABASES key was missing. On this project, I used the same database for testing the code, so it's not an entirely arbitrary configuration.
Related
I have Django project which is working fine in production(using Mysql as Database). I am running Mysql server locally in my PC(using XAMPP) , i have a done appropriate changes in setting.py as shown below.
BUT when i try to run "python manage.py migrate MYAPP" i am getting an error as shown below.
Also tried different command (same error)
--> python manage.py syncdb
--> python manage.py makemigrations MYAPP
--> python manage.py runserver
..etc.,
It is not creating any table in SQL backend , any suggestion ?
Settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'MYAPP',
'USER': 'root',
'HOST': '127.0.0.1',
'PORT': '3306',
}
}
ERROR
_mysql.connection.query(self, query)
django.db.utils.ProgrammingError: (1146, "Table 'MYAPP_DB.TABLErole' doesn't exist")
Suggesting your working on a current django release: python manage.py syncdb is deprecated. Instead use python manage.py migrate to apply django's core migrations.
Than make and apply your own migrations:
python manage.py makemigrations MYAPP
python manage.py migrate
To get a list of all available and applied migrations use:
python manage.py showmigrations
I want to deploy my django project to the production environments, and associated it with an new empty database, and I did as follows :
Create an new empty database
Updated settings.py and pointed the database name to the new one
Deleted the migrations folder under my App
Run python manage.py runserver and no errors returned
Run python manage.py makemigrations and python manage.py migrate
but only auth related tables created ( like auth_user , auth_group ... ), no databases tables created for my Apps
How should I do for this situation to move to the new database for my project?
Deleted the migrations folder under my App
This was your mistake, you deleted the migrations - including the initial migrations. So when you go to makemigrations you haven't got the initial migration available.
So you need to run makemigrations <app_name> to at least get the initial migration.
If you were to do this again, don't delete the migrations, just change the database settings and then migrate.
Firstly, you should not have deleted the migrations. Now, make all the migrations again which you have deleted.
python manage.py makemigrations app_name
Do this for all the apps of which you have deleted the migrations.
Now, add your new database to settings.py. Do not remove the old one yet. For example, if I were adding a MySQL database, I would have added the following to the DATABASES dictionary in settings.py:
'new': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'databasename',
'USER': 'databaseusername',
'PASSWORD': 'databasepassword',
'HOST': 'localhost',
'PORT': '3306',
}
I have named the database as 'new'. Now we have two databases 'default' and 'new'. Now you have to create tables in the new database by running the migrations on the new database:
python manage.py migrate --database=new
You can follow these additional steps if you want to transfer your data to the new database. First, clear the new database:
python manage.py flush --database=new
Now export data from the old database into a json file:
python manage.py dumpdata>data.json
Import this data into the new database:
python manage.py loaddata data.json --database=new
Now you can remove the 'default' database and rename the 'new' database to 'default'.
The procedure mentioned in this answer is taken from my blog.
Just check the output of python manage.py makemigrations command, if it is showing no change detected then you need to check that have you added that app in your INSTALLED_APPS = [] in settings.py file or it might be the problem because you have deleted migration folder.Because if is there any database connectivity error it will show you that while doing makemigrations.
If your database has a new name, i.e. not "default", you need to specify it to migrate:
python manage.py migrate --database <newdb>
How do I makemigrations on a stand alone Django app (ie one that is not part if any project).
For example after following: https://docs.djangoproject.com/en/1.8/intro/reusable-apps/
You can do it similar to how you do testing scripts for apps:
#!/usr/bin/env python
import sys
import django
from django.conf import settings
from django.core.management import call_command
settings.configure(DEBUG=True,
INSTALLED_APPS=(
'django.contrib.contenttypes',
'your_app',
),
)
django.setup()
call_command('makemigrations', 'your_app')
What I do is to create a mock project, containing only that app, then the process is as usual:
manage.py makemigrations myapp
I've got a Django app with South installed. I added an app ("guardian") in my INSTALLED_APPS. Now when I run python manage.py syncdb I get:
$ python manage.py syncdb
Syncing...
Creating tables ...
Installing custom SQL ...
Installing indexes ...
No fixtures found.
Synced:
> django.contrib.auth
> django.contrib.contenttypes
> django.contrib.sessions
> django.contrib.sites
> django.contrib.admin
> south
Not synced (use migrations):
- myapp
- guardian
and trying the migration returns:
Nothing seems to have changed.
Is there any way to use the original syncdb, not the South one?
Thanks
Are you running manage.py migrate guardian?
"Nothing seems to have changed" looks like an output of running manage.py schemamigration, which wouldn't create any tables to begin with...
You can remove "south" from INSTALLED_APPS then run syncdb and finally, add "south" to INSTALLED_APPS again.
I am pretty sure that database has been created and I have executed python manage.py syncdb properly but still i encounter this error when trying to access http://127.0.0.1:8000/admin/. Here are some details.
Request Method: GET
Request URL: http://127.0.0.1:8000/admin/
Django Version: 1.3.1
Python Version: 2.7.1
Installed Applications:
['django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'blog',
'django.contrib.admin']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')
Exception Type: DatabaseError at /admin/
Exception Value: no such table: django_session
path to sqlite is usually the problem.
'NAME': 'c:/path/to/sqlite.db'
Make migrations will solve the problem.
Run below commands:
python ./manage.py migrate
python ./manage.py makemigrations
Try this
from os.path import dirname, abspath
ROOT = dirname(abspath(__file__)).replace('\\', '/') + '/'
print "self.__name__: " + __name__
print "self.__file__: " + __file__
print "ROOT: " + ROOT
import django
print "django.__path__: "
print (django.__path__)
# Contact for error messages etc. - dont forget the ',' after the first entry
ADMINS = (('dev', 'dev#example.com'),)
MANAGERS = ADMINS
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': ROOT + 'project.db',
'USER': '', 'PASSWORD': '',
'HOST': '', 'PORT': '',
}
}
and check if you need + '/' + before the database name for your operating system.
SESSION_ENGINE it's using django.contrib.sessions.backends.db by default and that's make you need to :
put django.contrib.sessions in INSTALLED_APPS list variable in settings.py
you must migrate your database
if you don't want to do that, just put SESSION_ENGINE to django.contrib.sessions.backends.cache in your settings.py.
so, in your settings.py like this :
...
SESSION_ENGINE = 'django.contrib.sessions.backends.cache'
...
by the way, as in the documentation say :
'..the simple cache is faster because it disregards persistence.'
you can cek in this link
If you have added/updated table in any models.py file, you might need to migrate the db before running the server.
Run below commands before running 'python manage.py runserver':
python manage.py migrate
python manage.py makemigrations
That happens when you run the server without a database created or the session table inside of it (in case you recently add the admin app to your INSTALLED_APPS). In order to create the database or the table run this command.
python manage.py migrate
In older versions of django is this command
python manage.py syncdb
It prompted you to that error page because your Database wasn't even created, which lead to the absence of the mandatory tables to access the admin page.
Run the following two commands :
python3 manage.py migrate
python3 manage.py makemigrations
Running the first command actually creates your database which holds the necessary starter tables. I had the same problem and this solved 100% for me.
Solution source : #satya 's answer
Executing in the below order works fine:
First
python manage.py migrate
Then
python manage.py runserver.
Since we are applying all migrations before running the server.