I am trying to load data from a db.json file I created (Sqlite db was the source). The original database is Sqlite, and I am try to migrate to Mysql.
Everything works fine under Sqlite. I get the following error:
django.db.utils.OperationalError: Problem installing fixture '/home/balh/blah/db.json': Could not load trackx_site.Segment(pk=1): (1054, "Unknown column 'program_id' in 'field list'")
It seems like the Mysql tables do not have the foreign key or something?.... The models look like this:
class Program(models.Model):
air_date = models.DateField(default="0000/00/00")
air_time = models.TimeField(default="00:00:00")
service = models.CharField(max_length=10)
block_time = models.TimeField(default="00:00:00")
block_time_delta = models.DurationField(default=timedelta)
running_time = models.TimeField(default="00:00:00",blank=True)
running_time_delta = models.DurationField(default=timedelta)
remaining_time =
models.TimeField(default="00:00:00",blank=True)
remaining_time_delta = models.DurationField(default=timedelta)
title = models.CharField(max_length=190)
locked_flag = models.BooleanField(default=False)
locked_expiration = models.DateTimeField(null=True,default=None,blank=True)
deleted_flag = models.BooleanField(default=False)
library = models.CharField(null=True,max_length=190,blank=True)
mc = models.CharField(max_length=64)
producer = models.CharField(max_length=64)
editor = models.CharField(max_length=64)
remarks = models.TextField(null=True,blank=True)
audit_time = models.DateTimeField(auto_now=True)
audit_user = models.CharField(null=True,max_length=32)
class Segment(models.Model):
class Meta:
ordering = ['sequence_number']
program = models.ForeignKey(Program,
on_delete=models.CASCADE,
related_name='segments', # link to Program
)
sequence_number = models.DecimalField(decimal_places=2,max_digits=6,default="0.00")
title = models.CharField(max_length=190, blank=True)
bridge_flag = models.BooleanField(default=False)
seg_length_time = models.TimeField()
seg_length_time_delta = models.DurationField(default=timedelta)
seg_run_time = models.TimeField(default="00:00:00",blank=True)
seg_run_time_delta = models.DurationField(default=timedelta)
seg_remaining_time = models.TimeField(default="00:00:00",blank=True)
seg_remaining_time_delta = models.DurationField(default=timedelta)
author = models.CharField(max_length=64,null=True,default=None,blank=True)
voice = models.CharField(max_length=64,null=True,default=None,blank=True)
library = models.CharField(max_length=190)
summary = models.TextField()
audit_time = models.DateTimeField(auto_now=True)
audit_user = models.CharField(null=True,max_length=32)
I also get a error when I attempt to run the server with the Mysql db settings. It will not allow me to add a record into the model and gives me an error complaining about program_id. I dont have anything named program_id that I am aware of.. (I looks through all the code).
It works fine when I run with Sqlite db settings. Adds programs and segments just fine.
Don't know what's wrong here. Any idea what I am doing wrong?
I figured out that my mysql database was not migrating correctly. Part of the problem was it was trying to do all the migrations, but since it was the inital build of the new database I did not need to have all the migrations (and somehow something was happening correctly in there). Removed all of the migrations, did initial makemigrations and migrate, and my Foreign Key was created correctly, no more problems.
I am trying create a new database entry using a custom Django model I created. However, when I try to create the model and save it, the id does not increment. Instead, the previous database entry is overwritten whose id == 1. I have tried setting force_insert=True inside the save() function, but it results in a runtime error where the primary key already exists. I don't set any primary values in the creation of the object, so I'm not sure why the id is not being incremented. I am running the test code in the manage.py shell. All the models have been migrated properly.
The model:
class RoadWayData(models.Model):
blocked_lanes = models.PositiveIntegerField()
city = models.CharField(max_length=255)
county = models.CharField(max_length=255)
direction = models.CharField(max_length=255, blank=True, null=True, default=None)
eto = models.CharField(max_length=255)
incident_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
incident_object = GenericForeignKey('incident_type', 'id')
injuries = models.PositiveIntegerField()
postmile = models.CharField(max_length=255, blank=True, null=True, default=None)
queue = models.CharField(max_length=255, default="NONE: Freeflow Conditions")
route = models.CharField(max_length=255, blank=True, null=True, default=None)
street = models.CharField(max_length=255, blank=True, null=True, default=None)
timestamp = models.DateTimeField(auto_now=True)
update = models.PositiveIntegerField()
maintenance = models.CharField(max_length=255)
tow = models.CharField(max_length=255)
weather = models.CharField(max_length=255)
vehicles_involved = models.PositiveIntegerField()
The test code:
from incident.models import *
import datetime
x = IncidentIndex.objects.get(id=1)
y = CHPIncident.objects.get(id=x.incident_object.id)
print("ID already exists in DB: {}".format(RoadWayData.objects.get(id=1).id))
z = RoadWayData(
blocked_lanes=0,
city="testCity",
county="testCounty",
direction="NB",
eto="Unknown",
highway_accident=True,
incident_object=y,
injuries=0,
postmile="New Postmile",
route="new Route",
update = 2,
maintenance= "Not Requested",
tow="Not Requested",
weather="Clear Skies",
vehicles_involved=0,
)
z.save()
print("New Data Object ID: {}".format(z.id))
Shell Output:
ID already exists in DB: 1
New Data Object ID: 1
Edit #1:
I am using a mySQL database and have not overridden the save() function. The mySQL console shows only one entry in the table(the model that was most recently saved).
Edit #2
I commented out the RoadWayData model and migrated the changes to wipe the table. Afterwards, I un-commented the model and migrated the changes to add it back to the database. The issue still persists.
Edit #3
I was able to manually insert a new entry into the table using the mySQL console. The ID incremented correctly. Perhaps it is a Django bug?
Edit #4
I've pinpointed the source of the problem. The problem stems from the contenttypes library. More specifically, the GenericForeignKey. For some reason when an the content object is assigned, the model inherits the content object's id.
Code with problem isolated:
x = IncidentIndex.objects.get(id=1)
y = CHPIncident.objects.get(id=x.incident_object.id)
r = RoadWayData(
...
incident_object = None, # Do not assign the generic foreign key
...
)
r.save()
print(r) # Shows <RoadWayData object> with CORRECT id
r.incident_object = y # Assign the general object
print(r) # Shows <RoadWayData object> with the id of y. INCORRECT
The easiest fix would be to create a variable to keep track of the Model's id BEFORE assigning the content_object (incident_object in my case).
FIX:
... initialization from code above ...
r.save()
r_id = r.id # SAVE THE CORRECT ID BEFORE ASSIGNING GENERIC FOREIGN KEY
r.incident_object = y # ASSIGN THE GENERIC FOREIGN OBJECT
r.id = r_id # OVERWRITE THE ID WITH THE CORRECT OLD ID
r.save()
The incident_object field in the RoadWayData model, has the reference id (the second parameter) set to its own id. So, when model assigns incident_object , it overwrites the id of the model.
To fix it, create a new PostiveIntegerField (like incident_id) and replace
incident_object = GenericForeignKey('incident_type', 'id')
with
incident_id = models.PostiveIntegerField(null=True)
incident_object = GenericForeignKey('incident_type', 'incident_id')
I must be doing something very wrong or this error doesn't make any sense to me. I have an object Location:
class Location(models.Model):
class Meta:
db_table = 'location'
location_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
name = models.CharField(max_length=200)
state = models.CharField(max_length=200)
country = models.CharField(max_length=200)
apt_number = models.CharField(max_length=200, null=True, blank=True)
street_address = models.CharField(max_length=200)
city = models.CharField(max_length=200)
zip_code = models.IntegerField()
created = DateTimeField(auto_now_add=True)
here this is my code for inserting a new or updating an existing location record:
class LocationList(generics.ListCreateAPIView):
queryset = Location.objects.all()
serializer_class = LocationSerializer
def post(self, request, *args, **kwargs):
location_dict = request.data
if 'location_id' not in location_dict:
okStatus = status.HTTP_201_CREATED
else:
okStatus = status.HTTP_200_OK
location = Location(**location_dict)
location.save()
return Response(LocationSerializer(location).data, status=okStatus)
Inserts work fine, but everytime an update happens, I get the error "Column 'created' cannot be null". My online research seems to point me to the fact that this was a bug which has been long fixed. I expect the update to pass since the 'created' field was set to auto_now_add, which means Django should set that field once upon insert and leave it on any subsequent update. I do not know why Django is trying to set that column to null or any other value on update, because I expect Django to not update the column at all. I am using MySQL as database.
I think that your problem is in the " created column " try this steps :-
1) add null=True inside the created column
2) run :$ python"select version" manage.py makemigrations "appName or keep it empty"
3) run :$ python"V" manage.py sqlmegrate "aapName" " file version" >
check the file version in the app directory type it like this "0001"
4) run :$ python"V" manage.py migrate "appName"
after these steps ur db should be updated, last step is to remove (null=True from created column ) and start project.
I am writing a script which can pull data from different API's and store into a MySQL database. This application will run from command line. So I am only using Django's ORM.
But when I am creating a model which doesn't have primary key but have a column named id. When I am trying to save data in this model I am getting a error django.db.utils.IntegrityError: (1048, "Column 'id' cannot be null")
I am really confused why this happens. Because from API I get values from id column and there is no chance to get null or empty value for this column.
Please suggest me what I am doing wrong here.
Here is my model:
class Graphite(models.Model):
class Meta:
db_table = 'graphite'
id = models.BigIntegerField()
full_name = models.CharField(max_length=250, null=True)
email = models.CharField(max_length=250, null=True)
status = models.CharField(max_length=150, null=True)
And this is the code when I am trying to save data in this model:
Graphite.objects.using('database_name').create(
id=row['id'],
full_name=row['full_name'],
email=row['email'],
status=row['status'])
When saving data into model I am using Graphite.objects.using('database_name'). because I have multiple database connected in this application.
Well I'm not sure did you use django migrations, but it won't let you create this kind of model in django, where your id property (in model) hasn't primary key as its parameter (mySQL). So why don't you just define:
class Graphite(models.Model):
class Meta:
db_table = 'graphite'
id = models.BigIntegerField(primary_key=True)
full_name = models.CharField(max_length=250, null=True)
email = models.CharField(max_length=250, null=True)
status = models.CharField(max_length=150, null=True)
so set primary_key on id? Then you wouldn't have to pass id when creating Graphite.
BUT
If you have to provide id which is something you need to have in every Graphite model and it's something different than primary key, then just define it different, let's say row_id. But you should still have at last one id property in your model with primary_key set to True when you want to have id as BigIntegerField.
EDIT (on the example)
In mySQL execute this command:
ALTER TABLE graphite ADD COLUMN row_id BIGINT;
Then your model should looks like this:
class Graphite(models.Model):
class Meta:
db_table = 'graphite'
row_id = models.BigIntegerField()
full_name = models.CharField(max_length=250, null=True)
email = models.CharField(max_length=250, null=True)
status = models.CharField(max_length=150, null=True)
And usage:
Graphite.objects.using('database_name').create(
row_id=row['id'],
full_name=row['full_name'],
email=row['email'],
status=row['status'])
and that's it.
The problem is that you do not have a primary key.
From the docs:
Each model requires exactly one field to have primary_key=True (either explicitly declared or automatically added).
So, you have to make your id field a primary key by adding primary_key=True. Then, it won't complain.
You are overriding id from default django table id.
so there is no id for primary key. Just make it primary=True. or use another id like graphaite_id
You are missing your primary key, make sure you have your primary=True and to store your id make another column for it
I have the following models in models.py:
class ListinoTraduttore(models.Model):
traduttore = models.ForeignKey('Traduttore', related_name='Traduttore')
linguaDa = models.ForeignKey(Lingua, related_name = "linguaDa")
linguaA = models.ForeignKey(Lingua, related_name = "linguaA")
prezzoParola = models.CharField(max_length=50, blank=True)
prezzoRiga = models.CharField(max_length=50, blank=True)
scontoCat = models.CharField(max_length=50, blank=True)
scontoFuzzy = models.CharField(max_length=50, blank=True)
scontoRipetizioni = models.CharField(max_length=50, blank=True)
class Meta:
verbose_name_plural = "Listini Traduttori"
def __unicode__(self):
return u"%s Da %s A %s Parola=%s Riga=%s ScontoCAT=%s ScontoFuzzy=%s ScontoRipetizioni=%s" % (self.traduttore, self.linguaDa, self.linguaA, self.prezzoParola, self.prezzoRiga, self.scontoCat, self.scontoFuzzy, self.scontoRipetizioni)
class Traduttore(models.Model):
nome = models.CharField(nomeString, max_length=50)
cognome = models.CharField(cognomeString, max_length=50)
nomeAzienda = models.CharField(nomeAziendaString, max_length=50, blank=True)
codiceFiscale = models.CharField(codiceFiscaleString, max_length=50, blank=True)
partitaIva = models.CharField(partitaIvaString, max_length=50, blank=True)
indirizzo = models.CharField(indirizzoString, max_length=50, blank=True)
telefono = models.CharField(telefonoString, max_length=50, blank=True)
fax = models.CharField(faxString, max_length=50, blank=True)
email = models.EmailField(max_length=50, blank=True)
referente = models.CharField(referenteString, max_length=50, blank=True)
valuta = models.ForeignKey(Valuta)
metodoPagamento = models.ForeignKey(MetodoPagamento)
datiBancari = models.CharField(datiBancariString, max_length=50, blank=True)
programmiUtilizzati = models.ManyToManyField(Programma, blank=True)
note = models.CharField(max_length=200, blank=True)
listino = models.ManyToManyField(ListinoTraduttore, related_name='listino', blank=True)
def __unicode__(self):
return u"%s %s %s" % (self.nome, self.cognome, self.nomeAzienda)
class Meta:
verbose_name_plural = "Traduttori"
While in the admin.py I have the following:
class TraduttoreAdmin(admin.ModelAdmin):
list_display = ("nome", "cognome", "nomeAzienda")
search_fields = ["nome", "cognome", "nomeAzienda"]
class ListinoTraduttoreAdmin(admin.ModelAdmin):
list_display = ("traduttore", "linguaDa", "linguaA", "prezzoParola", "prezzoRiga", "scontoCat", "scontoFuzzy", "scontoRipetizioni")
search_fields = ['traduttore__nome", "linguaDa", "linguaA"]
But when I try to make a search in the admin page in the ListinoTraduttore table I have the following error:
TypeError at /admin/itrad/listinotraduttore/
Related Field has invalid lookup: icontains
Request Method: GET
Request URL: http://127.0.0.1:8000/admin/itrad/listinotraduttore/?q=Fenicio
Django Version: 1.4.1
Exception Type: TypeError
Exception Value:
Related Field has invalid lookup: icontains
Exception Location: /Library/Python/2.7/site-packages/django/db/models/fields/related.py in get_prep_lookup, line 142
Python Executable: /usr/bin/python
Python Version: 2.7.2
Python Path:
['/Users/nicolac/Documents/DjangoProjects/mysite',
'/Library/Python/2.7/site-packages/pip-1.1-py2.7.egg',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages',
'/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old',
'/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload',
'/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/PyObjC',
'/Library/Python/2.7/site-packages']
This is to (hopefully) simplify the answer.
Don't filter on a ForeignKey field itself!
Change this
search_fields = ['foreignkeyfield']
to this (notice TWO underscores)
search_fields = ['foreignkeyfield__name']
name represents the field-name from the table that we have a ForeignKey relationship with.
Have you tried adding the __fieldname on those Lingua references in the ListinoTraduttoreAdmin search_fields, like:
class ListinoTraduttoreAdmin(admin.ModelAdmin):
list_display = ("traduttore", "linguaDa", "linguaA", "prezzoParola", "prezzoRiga", "scontoCat", "scontoFuzzy", "scontoRipetizioni")
search_fields = ['traduttore__nome", "linguaDa__field1", "linguaA_field2"]
Use Django's double underscore convention instead. docs
foreignkeyfield__name
Make sure you are not adding any Foreignkey or ManyToManyField to your search_field directly.
class ListinoTraduttoreAdmin(admin.ModelAdmin):
list_display = ("traduttore", "linguaDa", "linguaA", "prezzoParola", "prezzoRiga", "scontoCat", "scontoFuzzy", "scontoRipetizioni")
search_fields = ['traduttore__nome", "linguaDa__field1", "linguaA__field2"]
Double underscore needed
class exampleAdmin(admin.ModelAdmin):
search_field = ('yourforeignkeyname__choosefieldnameinyourforeignkey')
This worked for me.
Search the field of the foreign key using my_related_object__first_attribute:
search_fields = ('author__username', 'title')
from models
author = models.ForeignKey(User, on_delete=models.CASCADE, related_name='blog_posts2')
This error, mostly occurs when you try to filter using a ForeignKey. I think the error is in the search_filelds. Check it. search_fields = ['traduttore__nome", "linguaDa", "linguaA"]. This two ForeignKey ("linguaDa", "linguaA") are the problem. Remove them. I think this helps.
This may not answer the original question, but, every so often I run into a similar invalid lookup error because I accidentally used _set in a lookup, e.g. <model_name>_set instead of just <model_name>.
Basically, I tend to confuse the related_query_name with the default_related_name
, which does include _set (also see queries docs and related manager docs).
From the lookups documentation:
It works backwards, too. Whilst it can be customized, by default you refer to a “reverse” relationship in a lookup using the lowercase name of the model.
(my emphasis)
Confusing thing is that the default related_name (i.e. <model_name>_set) is not the same as the default related_query_name (i.e. <model_name>), but if you set a custom related_name (or default_related_name, via model Meta options), that will also be used as the default related_query_name (as mentioned in the docs).
it may be weird
search_fields = ['traduttore__nome']
giving like this , with single quotes will create error.
search_fields = ["traduttore__nome"]
giving with double quotes will fix the issue
foreignkeyfield__lookupfield - this is the format
Please check if the field either exists in the model or not, if not, then remove the search field from the search list.
Then, check if the search field value is either a foreign key or not, if any field is a foreign key, then please add the foreign key's field name like ForeignKey__FieldName (FieldName is the very field you wanna search).
add in admin.py
admin.site.register(Traduttore, TraduttoreAdmin)
admin.site.register(ListinoTraduttore, ListinoTraduttoreAdmin)
see the link https://docs.djangoproject.com/en/dev/intro/tutorial02/