I'm trying to follow this tutorial but I'm stuck on the 5th step.
When I execute
[~/Django Projects/netmag$] python manage.py syncdb
I get the following error message :
Unknown command: 'syncdb'
Type 'manage.py help' for usage.
and here is the output of ./manage.py help does not contain syncdb command. How do I add it?
Thanks for any help!
Edit :
When I run migrate, I get this error :
"Error creating new content types. Please make sure contenttypes "
RuntimeError: Error creating new content types. Please make sure
contenttypes is migrated before trying to migrate apps individually.
in settings.py :
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.admindocs',
'blog',
]
Edit 2:
If I remove 'blog', from settings.py :
:~/Django Projects/netmag$ python manage.py migrate blog
CommandError: App 'blog' does not have migrations.
:~/Django Projects/netmag$ python manage.py makemigrations blog
App 'blog' could not be found. Is it in INSTALLED_APPS?
syncdb command is deprecated in django 1.7. Use the python manage.py migrate instead.
You have to use python manage.py migrate instead rather than python manage.py syncdb
Run python manage.py makemigrations result below
Migrations for 'blog':
blog/migrations/0001_initial.py:
- Create model Blog
and after that run python manage.py migrate result below
Operations to perform:
Apply all migrations: admin, blog, auth, contenttypes, sessions
Running migrations:
Applying article.0001_initial... OK
the actual command is :
python manage.py migrate --run-syncdb
It will solve many errors in django like , Operational error ,No Table found in databse etc.
You can do it like this in stages, let's say you have an app called "example":
Run python manage.py makemigrations example
A number generates like '0001' get the number
Run python manage.py sqlmigrate example 0001, using the number. Check out the scripts.
Run python manage.py migrate example 0001
You can also look at all your migrations like this: python manage.py showmigrations.
If you don't want to commit it, go to the folder and move it somewhere or delete it before doing step 4.
However, there is another error that can be happened as strict mode needs to be enabled for MariaDB.
Keep Database connection in the settings.py file as follows:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'test',
'USER': 'root',
'PASSWORD': '',
'HOST': 'localhost', # Or an IP Address that your DB is hosted on
'PORT': '3306',
'OPTIONS': {
'sql_mode': 'traditional',
}
}
}
keep in mind about the below code:
'OPTIONS': {
'sql_mode': 'traditional',
}
After all, if your DJango version is backdated, "python manage.py syncdb" will work but for an updated version more than or equal to 1.7, please use "python manage.py migrate"
Thanks
Related
What can I make with it? I'm beginner in python and django. I download it and I i wrote py manage.py makemigrate and I've get error. Can u help me?
Your issue is with your DB configuration in the setting.py. If you are using the default SQLite then copy/paste this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
}
}
and your project will work just fine. After this, run
python manage.py makemigrations
python manage.py migrate #copy all migrations to the database
python manage.py createsuperuser #to have a admin user to login to adminpanel
python manage.py runserver #starting the server
Otherwise, take a look at the official documentation how to connect MySQL, PostgreSQL, Oracle databases and required configurations.
Your error is in here:
SQLite is not like MySQL or other databases. Actually, it is not a real database. You are using a port, username, password and etc. These are the cause of the error. SQLite is not running in the server or another place. It is just a single file contains data information. Update yours to mine above and it should start work again or change your database to MySQL or others.
You need to supply all environment variables that are listed in your settings file. Such as DB_NAME that presented in your screenshot. Search for os.environ[<VARIABLE_NAME>], every VARIABLE_NAME should be defined.
If you are a beginner it is better to stay with the documentation and do like https://docs.djangoproject.com/en/2.1/intro/tutorial01/
If you could share the DB part of the settings.py it would help.
Generally python manage.py startapp appname should create the necessary files for you.
After which a python manage.py makemigrations and python manage.py migrate should work properly. And this should not come.
Got this error after changing my database from sqlite to postgresql. I've made all my settings changes:
Here's my settings:
DATABASES = {
'default': {
'ENGINE': "django.db.backends.postgresql_psycopg2",
'NAME': "postr1",
'USER': "zorgan",
'PASSWORD': config('DB_PASSWORD'),
'HOST': "localhost",
'PORT': '',
}
}
as well as performing makemigrations and migrations which were all successful. So I'm able to succesfully start my local server:
System check identified no issues (0 silenced).
May 15, 2018 - 08:59:39
Django version 1.11.8, using settings 'draft1.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
however when I go to the site it returns this error:
ProgrammingError at /news/
relation "django_session" does not exist
LINE 1: ...ession_data", "django_session"."expire_date" FROM "django_se...
Any idea what the problem is?
Try fake migrate to zero.
Your migration history shows that sessions table was already made, but you don't have real table.
so following below
python manage.py migrate --fake sessions zero
# then your sessions migrate will be
python manage.py showmigrations
sessions
[ ] 0001_initial
# then migrate with --fake-initial again
python manage.py migrate --fake-initial
Then try again.
I'm using django-v-3
Here is the answer how to solve this issue?
1. python manage.py migrate --fake
2. python manage.py migrate --fake-initial
3. Then write python manage.py runserver
Enjoy
If facing issue use python manage.py help. I hope that you will get the solution.
So I'm trying to run the initial migrations on a django app and when I try to run the migrate command (python manage.py migrate or makemigrations) I get the following error:
psycopg2.ProgrammingError: relation "dotworks_server_internship" does not exist
LINE 1: ...s", "dotworks_server_internship"."questions" FROM "dotworks_...
^
I'm on a Windows environment using Django 1.9.6 and my database is postgres. Plus, I'm using PGAdmin to manage my database.
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'dotworks',
'USER': 'postgres',
'PASSWORD': 'mypasswordgoeshere',
'HOST': 'localhost',
'PORT': '5432',
}
}
I had this problem and I had to comment out everything in urls.py that referenced views.py, then run makemigrations. Hope this helps.
Make sure that you don't have any class variables in your code that are calling Django manager
For example:
class SomeViewSet(viewsets.ViewSet):
se = SomeEntity.objects.first() # fetching some entity on the class level
def list(self, request):
# the rest of the code
So, when you try to create/apply migrations, this variable will also try to initialise, and will try to access SomeEntity, but at that moment that entity doesn't even exist, and the error occurs.
If all other solutions mentioned fail, if you are still in development probably the easiest solution is dropping the database (in pgAdmin 4 2.0, right-click on database) and then run makemigrations and migrate.
Try to migrate particular app using following process. Refer Django migrations
python manage.py makemigrations
Initial migration created then run migrate command with app name
python manage.py migrate appname1, appname2
When you run a query before applying migrations this error appears.
If you got this error during python manage.py makemigrations or python manage.py migrate you must consider that makemigrations and migrate commands run after successful django bootstrap! So this error happens when you run a query during django bootstrap! So you must find the place you run this query during bootstrap progress.
For example, during bootstrap, django reads root {project}/urls.py and its nested imports. If you use views or viewsets in urls.py and they are run a query during initializing (in their __init__ method or __init__.pyfile or somewhere etc.), it happens!
In this situation and similars, you must comment out any entry in urls.py and similar files which cause running a query during bootstrap and prevent them from running by raising of exception during bootstrap! makemigrations and migrate need successful bootstrap to be run!
If your commented out code needs to makemigrations and migrate handcooks :D, it needs to be patient and be silent for a cycle or a while ;), and after a successful migrations it could be active and verbose ;D.
Your app is trying to call some DB entries that does not exist.
If you are trying to migrate it to a new database, one of your options is to export a dump of old database and import it to your new DB.
For example in PostgreSQL, import the database using below command then migration will work!
sudo -u postgres -i psql mydb < mydb-export.sql
If you're running in local,
For each Django app (maybe you have only one), erase the content of the migrations folder.
Then, run python manage.py makemigrations app1 app2 app3 (if you have 3 Django apps named app1, app2, app3). This will (re)create the migrations files required to migrate your database
Then, run python manage.py migrate. It will apply the migration files you just created.
This error may have related to previous database error.so if you created new database and you also face that type of error ,you can simply run the command with the app name:
1)python manage.py makemigrations <"app name">
2)python manage.py migrate <"app name">
I've solved this error with this solution.
first remove all url in urls.py .
create simple function view for viewing nothing.
def simple(request):
context = {}
return render(request, 'base.html', context)
and add url to urs.py
do migrate
python manage.py migrate
after migrate,
recover the deleted urls.py contents
:)
For me the error came from some initialization code I put into the app.ready() method. Commenting that part of code allowed me to run the command makemigrations without any issue.
I believe app.ready is called at some point by manage.py even for the makemigrations command, which caused my code to query my database before any migration.
I found the problematic code thanks to the traceback.
I first ran
python manage.py syncdb
and it created the database and tables for me, then I tried to add more apps, and here's what I did:
create apps by
python manage.py startapp newapp
Then I added 'newapp' to INSTALLED_APPS in setting.py:
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'newapp',
)
At last I ran syncdb:
python manage.py syncdb
and here's the result I get:
Creating tables ...
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)
I checked my db and there is no table named newapp, no table's name including newapp.
I also ran into this issue, and was able to get around it by removing the migrations folder inside my app. Somehow that had already gotten created and was tricking syncdb into thinking that it was fully migrated already, but those migration scripts didn't actually do anything useful. Obviously don't try this if you actually have migrations you want to save, but I was working with a brand new app and models.
If you run:
python manage.py inspectdb > somefile.txt
You can get quickly check out if your database structure is matching your django models.
I tried most of the ideas above:
making sure the models.py is imported (verified that the module executed during a makemigrate),
deleting the migrations folder
setting managed = True (this is default anyways),
used the python manage.py inspectdb (which correctly dumped the table, if I had created it manually).
The key was simply to run a makemigrations on the app separately:
python manage.py makemigrations <app_name>
as part of performing the makemigrations step. Then you do
python manage.py migrate
afterwards as usual.
(applies to Django 1.10, using Postgres 9.5).
Django documentation
Credit, related post
i got same problem, but i didn't have any "migration" folder. I solved it like below.
I just added app_label with the model code,
class MyModel(models.Model):
...
class Meta:
managed = True # add this
app_label = 'myapp' # & this
Also, make sure it is discoverable by referencing it in myapp/models/__init__.py
from model_file.py import MyModel
You should be using this command
python manage.py migrate
where you were runing syncdb from the location where manage.py resides
I was having the same problem and noticed that it had created a db.sqlite3 file that didn't seem empty:
$ ls -l
-rw-r--r-- 1 sauron staff 122880 Jul 31 01:22 db.sqlite3
I tried running sqlite again using the filename as an argument and that worked:
$ sqlite3 db.sqlite3
SQLite version 3.7.13 2012-07-17 17:46:21
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .schema
CREATE TABLE "auth_group" (
"id" integer NOT NULL PRIMARY KEY,
"name" varchar(80) NOT NULL UNIQUE
);
... many rows ...
In my Django app cannot activate models.
manage.py validate return 0 error, no matter what I do, even if I type wrong code on purpose.
manage.py syncdb does not sync anything.
How can I fix this?
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'YolaSite.consumer',
'south',
# Uncomment the next line to enable the admin:
'django.contrib.admin',
# Uncomment the next line to enable admin documentation:
# 'django.contrib.admindocs',
)
Here is DB connection:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'testdb', # Or path to database file if using sqlite3.
'USER': 'postgres', # Not used with sqlite3.
'PASSWORD': 'alexsis', # Not used with sqlite3.
'HOST': '127.0.0.1', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '5432', # Set to empty string for default. Not used with sqlite3.
}
}
Django's syncdb command only creates tables at first time, but doesn't support model evolution.
But as we can see you have already installed South, which is the right tool for database/schema migrations. The next step is that you need to tell South which models it has to take care of.
As long as you have only a development environment I would suggest to delete all tables of your own apps and then follow the instructions in the South tutorial.
In short:
First, tell South to watch the models of a specific app and create the initial migration:
./manage.py schemamigration your_app_name --initial
Second, apply this migration to your database:
./manage.py migrate your_app_name
Third, any upcoming changes to your models need a new migration, which can be generated like this:
./manage.py schemamigration your_app_name --auto
And every migration has to be deployed, so again:
./manage.py migrate your_app_name
For more information read the South docs. Hope this helps!
From your comment:
I've been able to activate models first time, so the version of the website (django app) is actually working, but now that I try to change models it seems that it is not being recognized.
You've successfully used manage.py syncdb to create the database table, and now you're trying to use it to "sync" changes. That isn't what syncdb does, though. The documentation (found here) for Django 1.4 reads:
Syncdb will not alter existing tables
syncdb will only create tables for models which have not yet been installed. It will never issue ALTER TABLE statements to match changes made to a model class after installation. Changes to model classes and database schemas often involve some form of ambiguity and, in those cases, Django would have to guess at the correct changes to make. There is a risk that critical data would be lost in the process.
If you have made changes to a model and wish to alter the database tables to match, use the sql command to display the new SQL structure and compare that to your existing table schema to work out the changes.
Which is why you aren't getting any errors. For database migration, use South, which you have installed. Read its documentation for it's usage.
You may use south,
or drop your table and syncdb again