I have, in models.py:
class Flashcard(models.Model):
english = models.TextField()
slavonic = models.TextField()
urls.py references models.py in an attempt to make python manage.py syncdb pick up on models.py:
import models
However, a python manage.py syncdb does not result in the creation of a *_flashcards table:
$ python manage.py syncdb && sqlite3 flashcards.db
No fixtures found.
SQLite version 3.7.13 2012-06-11 02:05:22
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .tables
auth_group auth_user_user_permissions
auth_group_permissions django_admin_log
auth_message django_content_type
auth_permission django_session
auth_user django_site
auth_user_groups
sqlite>
What should I be doing differently to get a flashcards_flashcards table? The admin interface picks up on it perfectly, up to displaying an "Add entry" page for the model.
Thanks,
You forgot to add your app to INSTALLED_APPS in the settings file, that's why syncdb command is not picking the models defined in that app.
Related
class UserSubStatus(models.Model):
msisdn = models.CharField(max_length=200)
category = models.CharField(max_length=200, null=True, blank=True)
validity = models.IntegerField(default=1,null=True,blank=True)
sub_status = models.IntegerField(default=0)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta:
ordering = ['-created_at']
db_table = "user_sub_status"
I add this table on my model.py file. How add this "user_sub_status" table on my database without losing any data.
Hello Dear
First run the following command:
python manage.py makemigrations your_app_name
than run this command:
python manage.py migrate your_app_name
NB: I hope, "user_sub_status" will be added to your database without losing data.
run makemigrations and migrate as you did before. If you do not erase previous migrations files then by running makemigrations new migrations files will add to migrations folder and by migrate new tables or columns for previous tables will add to your database and there will be no problem with your stored data
First, you have to run makemigrations command which is responsible for creating new migrations based on the changes you have made to your models.
python manage.py makemigrations
Then you have to run a migration command which is responsible for applying and unapplying migrations that are created by the above command.
python manage.py migrate
With the help of above both command, your table "user_sub_status" will be added in your database without losing any data from your database.
Whenever I try to migrate my application in Django, the python manage.py migrate command throws Operational errors that say:
django.db.utils.OperationalError: table "main_site_prescription" already exists
I tried deleting the table and that didn't work. I tried to --fake the migration, but then the database doesn't recognize the change to models.py. This is thrown on the site when I it tries to use one of the columns in a view:
Exception Type: OperationalError
Exception Value: table main_site_event has no column named initiated_by_id
Here are the tables:
sqlite> .tables
auth_group
auth_group_permissions
auth_permission
auth_user
auth_user_groups
auth_user_user_permissions
django_admin_log
django_content_type
django_migrations
django_session
main_site_appointment
main_site_doctor
main_site_doctor_listOfPatients
main_site_event
main_site_hospital
main_site_hospital_listOfDoctors
main_site_hospital_listOfHospitalAdmins
main_site_hospital_listOfNurses
main_site_hospital_listOfPatients
main_site_hospitaladmin
main_site_hospitaladmin_listOfDoctors
main_site_hospitaladmin_listOfHospitalAdmins
main_site_hospitaladmin_listOfNurses
main_site_hospitaladmin_listOfPatients
main_site_message
main_site_nurse
main_site_nurse_listOfDoctors
main_site_nurse_listOfPatients
main_site_patient
main_site_patient_prescriptions
main_site_patient_records
main_site_prescription
main_site_record
and here is the PRAGMA table_info(main_site_event) output...
sqlite> PRAGMA table_info(main_site_event);
0|id|integer|1||1
1|activity|text|1||0
2|time|datetime|1||0
sqlite>
This is the representation in the model...
'''Class representing an event, used for the activity log'''
class Event(models.Model):
activity = models.TextField(max_length=500, blank=True) # an action from the list of actions
time = models.DateTimeField(auto_now_add=True) # the time of the action in datetime format
initiated_by = models.ForeignKey(User, related_name="initiator") # the user who initiated the action
target_of = models.ForeignKey(User, default = None, related_name="target", null = True) # the user who is the target of the action
hospitals = models.ForeignKey(Hospital, default=None, null = True)
prescriptions = models.ForeignKey(Prescription, default=None, null = True)
Which obviously aren't correct (table missing multiple columns)
I created a db with such tracks.models:
class Song(models.Model):
title = models.CharField(max_length=30)
album = models.ForeignKey(Album)
def __unicode__(self):
return self.title
and used
python manage.py sqall tracks
python manage.py syncdb
but then I changed models to
class Song(models.Model):
songid = models.CharField(max_length=30)
title = models.CharField(max_length=30)
album = models.ForeignKey(Album)
def __unicode__(self):
return self.title
and did
python manage.py sqall tracks
python manage.py syncdb
again. Output of sqall:
BEGIN;
CREATE TABLE "tracks_song" (
"id" integer NOT NULL PRIMARY KEY,
"songid" varchar(30) NOT NULL,
"title" varchar(30) NOT NULL,
"album_id" integer NOT NULL REFERENCES "tracks_album" ("id")
)
;
CREATE INDEX "tracks_song_6781e42a" ON "tracks_song" ("album_id");
COMMIT;
syncdb:
Creating tables ...
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)
But whenever I tried to access tracks.models.Song.all() it said:
OperationalError: no such column: tracks_song.songid
So I decided to
python manage.py flush
python manage.py sqall tracks
python manage.py syncdb
(same output)
But problem hasn't gone and there's still no such column: tracks_song.songid.
What's the problem behind it?
python manage.py sqlall app-name will only print SQL sentences, not create or change database structures, that is to say, it's used to check or tell you what django actually do in databases. Howerver, django version<1.7 doesn't track changes of class in models.py(newly increased or delete existed class could be detected and syncdb), but you can use south, or django 1.7 to do such thing.
python manage.py flush will IRREVERSIBLY DESTROY all data, but not change tables in database.
South: http://south.aeracode.org/
Django 1.7: https://docs.djangoproject.com/en/dev/releases/1.7/#django-1-7-release-notes-under-development
you should also notice that if you only need an id field, class Song has a default AutoField id, just use song.id Django models Quick Example
I suspect that I have not imported my models into some place they need to be reported for a syncdb to pick them up.
Where do I need to import my models.py (or otherwise register my models) so that Django will include them on a syncdb?
In models.py, I have:
from django.contrib.auth.models import User
from django.db import models
class CalendarEntry(models.Model):
description = models.TextField()
interval = models.IntegerField()
regular_expression = models.TextField(blank = True)
scratchpad = models.TextField()
should_hide = models.BooleanField()
user = models.ForeignKey(User)
...
When I try to run a syncdb, it doesn't create any tables; the database is empty.
christos#christos-virtual-machine:~/dashboard > python manage.py syncdb
Creating tables ...
Installing custom SQL ...
Installing indexes ...
Installed 0 object(s) from 0 fixture(s)
christos#christos-virtual-machine:~/dashboard > sqlite3 *.db
SQLite version 3.7.15.2 2013-01-09 11:53:05
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .tables
sqlite>
What can/should I do so that the syncdb appropriately populates the database?
Is the app in INSTALLED_APPS ?
Also note that syncdb won't populate tables. It will only create them.
So this is my model:
class Config(models.Model):
config_key = models.CharField(max_length=50)
config_value = models.CharField(max_length=255)
I have created an initial migration with:
python manage.py schemamigration myapp --initial
But now I would like to insert some initial data inside the config table and create a migration that inserts that data (basically it will run on insert sql query).
How to do it?
python manage.py datamigration myapp name_migration