I have an already existing app with alot of database entries.
class Foo(models.Model):
value = models.TextField(u"Value")
For this I do this:
python manage.py schemamigration myapp --initial
python manage.py migrate myapp
I change the model to such:
class Foo(models.Model):
value = models.TextField(u"Value")
live = models.BooleanField(u"Live", default=False)
creation_time = models.DateTimeField("Creation Time", auto_now_add=True, null=True, blank=True)
and migrate:
python manage.py schemamigration myapp --auto
python manage.py migrate myapp
I get django.db.utils.DatabaseError: relation "myapp.foo" already exists error.
I have already checked this question but --fake doesn't seem to be supported via South anymore.
Your models look invalid to me, though I'd be surprised if that's what's actually causing the problem.
It looks like your first argument is intended to be the verbose_name attribute, your model should probably look like this:
class Foo(models.Model):
value = models.TextField(verbose_name = u"Value")
live = models.BooleanField(verbose_name = u"Live", default=False)
creation_time = models.DateTimeField(verbose_name = u"Creation Time", auto_now_add=True, null=True, blank=True)
(you also forgot the u before the verbose_name for creation_time).
Meanwhile, --fake is definitely still supported (see the docs), what error are you getting when you try and run it?
Related
After experimenting with setting up CustomUser and many-to-many relationships, I decided to make some (what turned out to be major) changes to the models. I had read that CustomUser/Auth needs to be set up first, or else it'll be a mess. But since I'm just learning and there was barely any data involved, I just went for it--not to mess up the CustomUser model intentionally, but I changed a model name--and (maybe this is the critical error) I updated the names everywhere it appeared. Now I'd deleted and reset the database, VENV folder, all the migration (along with __pycache__) files, and recreated test data so many times that I thought I could download the GitHub repo and set up a fresh new app easily again. But the app refuses to run and keeps asking for a missing table.
Re-creating the app isn't my main concern-I'd like to understand what happened-what is this missing table (see the snippet of the error code)? AND: Why is a perfectly intact repo having the same issue with the missing table?
Exception in thread django-main-thread:
Traceback (most recent call last):
File "/Volumes/Volume2/dev/lms6-v2/venv/lib/python3.9/site-packages/django/db/backends/utils.py", line 89, in _execute
return self.cursor.execute(sql, params)
File "/Volumes/Volume2/dev/lms6-v2/venv/lib/python3.9/site-packages/django/db/backends/sqlite3/base.py", line 477, in execute
return Database.Cursor.execute(self, query, params)
sqlite3.OperationalError: no such table: accounts_staff
The app is called accounts and the CustomUser models are:
class CustomUser(AbstractUser):
user_type_data = ((1, "HOD"), (2, "Staff"), (3, "Student"))
user_type = models.PositiveSmallIntegerField(default=1, choices=user_type_data)
email = models.EmailField(_('email address'), unique=True)
is_staff = models.BooleanField(default=False)
is_active = models.BooleanField(default=True)
date_joined = models.DateTimeField(default=timezone.now)
USERNAME_FIELD = 'email'
REQUIRED_FIELDS = []
objects = CustomUserManager()
def __str__(self):
return self.email
class AdminHOD(models.Model):
id = models.AutoField(primary_key=True)
admin = models.OneToOneField(CustomUser, on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now_add=True)
objects = models.Manager()
class Staff(models.Model):
id = models.AutoField(primary_key=True)
admin = models.OneToOneField(CustomUser, on_delete=models.CASCADE)
user_type = "Staff"
email = models.EmailField()
address = models.TextField(blank=True)
date_joined = models.DateTimeField(auto_now_add=True)
note = models.TextField(blank=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now_add=True)
objects = models.Manager()
def __str__(self):
return self.admin.first_name + " " + self.admin.last_name
def get_absolute_url(self):
return reverse('staff_detail', kwargs={'pk': self.pk})
first of all u need to delete all your migrations files(sure u have ) and the (pycache) ...
After you have done that dont just run makemigration u have to be logical with it
run python manage.py makemigrations <app_name:the app that contains the AUTH_USER_MODEL>
run python manage.py makemigrations <app_name> but this time u decide what app comes next in the way u wrote ur models they makemigrations in that order'
3)run python manage.py makemigrations
4)lastly run python manage.py migrations
... this how i solved this issue anytime i run into this problems
Obviously messing with the CustomUser model in Django isn't to be attempted casually, but this was a good learning experience. So for the newly initiated accidentally stuck at the same spot, the answer by Ogechuwku Matthew is a good start.
To elaborate-this is what I did:
Deleted all the migration files, including files in __pycache__ (EXCEPT for the __init__ file(s).
Disabled the AUTH_USER_MODEL = 'myapp.MyCustomUser' line in settings.py This step is key.
Removed (or commented out) all but the most essential files.
Commented out all but the CustomUser model and models extended from it.
Ran python manage.py makemigrations <app_name> and then 'python manage.py migrate`
After step 4 worked, ran python manage.py makemigrations <app_name> and python manage.py migrate again.
Moved the rest of the files back in, starting with the simplest bits, like 'HomeView' in views.py and 'home.html'.
When the app started up with 'home.html', un-commented models, views, and urls one cluster at a time.
Additionally: If error occur when running the app, first check the embedded dynamic links--they may extend or include files that aren't there yet-same with {% load static %}
I created a amount field in models.py,
I run python manage.py makemigrations and python manage.py migrate
It gave me error ValueError: Field 'amount' expected a number but got ''.
then I removed this amount field line from my code and again did makemigrations and migrate command. now again it it showing same error. when I open Django admin page and select my model name which is Orderss . It gives me error that there is no filed name amount
class Orders(models.Model):
order_id = models.AutoField(primary_key = True)
items_json = models.CharField(max_length=5000)
name = models.CharField(max_length=20)
# amount = models.IntegerField(default=0)
email = models.CharField(max_length=20)
address = models.CharField(max_length=50)
city = models.CharField(max_length=20)
state = models.CharField(max_length=20)
zip_code = models.IntegerField(max_length=6)
phone = models.IntegerField(max_length=12, default="")
def __str__(self):
return self.name
Delete your migrations and delete you db.sqlite3
and then
python3 manage.py makemigrations
python3 manage.py migrate
hope it work
When you did the migration first time, you’ve created the table in the database with ‘amount’ column. The second migration did’t remove this column. You can go to your database shell and remove the column there. It depends on the type of database you use, but you you might try to do it this way:
python manage.py dbshell
mysql> | psql> ALTER TABLE <table_name> DROP column <COLUMN_NAME>;
Then exit the shell and sync
python manage.py syncdb
1.In the Migration folder delete all files except __init__.py and then run
python manage.py makemigrations and
python manage.py migrate
I am writing a apps and run makemigrations and migrate multiple time when i add new field.
And now i have added a new foreignKey Field in models and then i run the command:
python3 manage.py makemigrations
after this, i run this command python3 manage.py migrate but it throws me following error
TypeError: an integer is required (got type str)
and later again i run this command pythion3 manage.py migrate --fake and i have successfully migrated and got another problem, when i go to django admin templates and click on my models to add data, it throws me this error:
OperationalError at /admin/booking/seats/
no such column: booking_seats.seat_for_id
I am not getting whats wrong with it, can anyone help me to fix this?
for your reference, see my models:
class Seats(models.Model):
alias = models.UUIDField(
primary_key=True,
default=uuid4,
editable=False
)
seat_no = models.IntegerField(
choices=SeatChoices.choices(),
)
availability = models.IntegerField(
choices=SeatAvailability.choices(),
default=SeatAvailability.AVAILABLE
)
seat_for = models.ForeignKey('booking.show', on_delete=models.CASCADE)
def __str__(self):
return str(self.seat_no)
class Show(models.Model):
alias = models.UUIDField(
primary_key=True,
default=uuid4,
editable=False
)
show_schedule = models.IntegerField(
choices=ShowSchedule.choices(),
)
movie = models.CharField(max_length=50)
def __str__(self):
return str(self.show_schedule)
I just added this new field seat_for = models.ForeignKey('booking.show', on_delete=models.CASCADE) then i facing the problem
Can anyone help me in this case?
I think the problem is that your model is not with a capital, as Python is case sensitive. It should be like this:
seat_for = models.ForeignKey('booking.Show', on_delete=models.CASCADE)
I would like to understand what I'm missing.
My model before cahnge:
class sectorToMapXY(models.Model):
pos = models.IntegerField()
name = models.CharField(max_length=15)
mapX = models.FloatField(default=0)
mapY = models.FloatField(default=0)
destMap = models.ForeignKey(Document, related_name='%(class)s_docfile')
Model after change (updated the on_delete delete in the destMap):
class sectorToMapXY(models.Model):
pos = models.IntegerField()
name = models.CharField(max_length=15)
mapX = models.FloatField(default=0)
mapY = models.FloatField(default=0)
destMap = models.ForeignKey(Document, related_name='%(class)s_docfile',on_delete=models.SET(-1))
When running python manage.py makemigrations it return No changes detected.
When changing the on_delete to SET_NULL or SET_DEFAULT it does detect the change.
Update:
I haven't set the SET_NULL nor SET_DEFAULT so when trying to run it returns error (but it detects the change):
SystemCheckError: System check identified some issues:
ERRORS:
dashboard.sectorToMapXY.destMap: (fields.E321) Field specifies on_delete=SET_DEFAULT, but has no default value.
HINT: Set a default value, or change the on_delete rule.
When adding the models.ForeignKey(Document, related_name='%(class)s_docfile',null=True,on_delete=models.SET_NULL)
running python manage.py makemigrations returns to No changes detected.
I wasn't able to reproduce your error on django 1.8.
However, I'm not sure how did the makemigrations command worked successfully when setting on_delete=SET_NULL or on_delete=SET_DEFAULT, since you are missing null=True or default=default_value on the field definition.
If this is the first time you handle migrations on your app, you should run:
$ python manage.py makemigrations your_app_label
As specified here.
In django model I had a class named Invitation.
I execute manage.py syncdb table Invitation is created.
This is my first table.
But when i create another class named Image and execute manage.py syncdb
it returns an error.
that Table UserInvitation already exists.
Please help me to create the table Image using syncdb command.
Models
class UserInvitation(models.Model):
InvitationID = models.AutoField(primary_key=True)
InvitationCode = models.CharField(max_length=255)
Email = models.CharField(max_length=150)
ExpireDate = models.DateTimeField()
Deleted = models.BooleanField(default=False)
CreatedDate = models.DateTimeField(auto_now=True)
class Meta:
db_table = u'UserInvitation'
class UserImage(models.Model):
ImageID = models.AutoField(primary_key=True)
UserID = models.IntegerField(unique=True)
FileName = models.CharField(max_length=255)
class Meta:
db_table = u'UserImage'
You should use South to reflect changes in your database.
syncdb runs commands that create all the tables in the DB, not just the one that you've added.
With South, you should revert your model back to when it only had the UserInvitation class, run the command:
python manage.py convert_to_south <name of app>
Add back the UserImage class and run the command:
python manage.py schemamigration --auto <name of app>
python manage.py migrate <name of app>
This would add the userimage table in your database.
Also, the db_table field in the meta classes should be in lowercase, as the table names, when created, are in lowercase (check your db to make sure)
See South documentation: http://south.readthedocs.org/en/latest/