Unkown Column error on django admin, how to fix it? - python

i have an error in my django admin:
(1054, "Unknown column 'flora2estado.id' in 'field list'")
the model flora2estado has two fields, they are used in unique together as a pseudo composite key, how can i fix this?
admin.py
admin.site.register(Flora2Estado)
models.py
estado = models.OneToOneField(Estados, models.DO_NOTHING, primary_key=True)
especie_id = models.IntegerField()
flora2estado = models.IntegerField(blank=True, null=True)
class Meta:
managed = False
db_table = 'flora2estado'
unique_together = (('estado', 'especie_id'),)
I tried to add the "flora2estado" field without sucess.
All migrations done, thank you for your time

Django is trying to select id field, as documented
By default, Django gives each model the following field:
id = models.AutoField(primary_key=True)
If you’d like to specify a custom primary key, specify
primary_key=True on one of your fields. If Django sees you’ve
explicitly set Field.primary_key, it won’t add the automatic id
column.

Related

How to prevent django from swapping primary key values

I am creating a Django web app. I have set a field as a primary key and a unique field. When I try to edit it and give it the same value as another item in the primary key column, the current item got duplicated and each value goes to one of them as shown below.
This is my code from models.py:
class Car(models.Model):
class Meta:
verbose_name = "Car"
verbose_name_plural = "Cars"
car_id = models.CharField(verbose_name='Car ID', max_length=10000, primary_key=True, unique=True)
Some screenshots from the admin panel:
Once you change the primary key, Django will execute an update statement for that id so corrputing your data, the best way as mentioned by #vishal-singh, is to set the id field as Read-only.

I can't query a table without primary key

I'm trying to query through a model in Django that has no Primary Key.
I need to query though it so I can access to the Foreign keys it has.
I'm just trying to do this atm and doesn't even work:
chars = Characterweapons.objects.all()
print(chars)
And if i change Characterweapons to Weapons for example, a table with Primary Key it works.
The error I get when I load the page is this:
Exception Value: (1054, "Unknown column 'characterweapons.id' in 'field list'")
This is my model:
class Characterweapons(models.Model):
characterid = models.ForeignKey(Characters, models.DO_NOTHING, db_column='CharacterID') # Field name made lowercase.
weaponid = models.ForeignKey(Weapons, models.DO_NOTHING, db_column='WeaponID', blank=True, null=True) # Field name made lowercase.
categoryid = models.ForeignKey(Category, models.DO_NOTHING, db_column='CategoryID', blank=True, null=True) # Field name made lowercase.
quantity = models.IntegerField(db_column='Quantity', blank=True, null=True) # Field name made lowercase.
class Meta:
managed = False
db_table = 'characterweapons'
def __str__(self):
return '%s %s %s %s' % (self.quantity,self.characterid,self.weaponid,self.categoryid)
Anyone knows about this?
Thanks in advance!
So it looks like you did a DBinspect on an existing database to generate this model. I'm guessing this is failing because Django ORM expects your table to have a primary key. "id" is the default name for a Django generated model primary key field. I suspect when you are trying to call Characterweapons.objects.all() it's trying to get the primary key field for some reason.
There may be a workaround but unless you really know what your doing with your database, I would highly urge you to set a primary key on your tables.
The best solution that I found, in this case, was to perform my own query, for example:
fact = MyModelWithoutPK.objects.raw("SELECT * FROM my_model_without_pk WHERE my_search=some_search")
This way you don't have to implement or add another middleware.
see more at Django docs

Django migration returns unexpected name

I am trying to migrate a database, but every time I try it returns the error below. My app name is 'helloservice', but the table is 'customer' in the database 'pulse'. How do I tell Django not to use the helloservice prefix? If needed I followed a tutorial here, and am now trying to adapt the results to my own needs.
django.db.utils.ProgrammingError: (1146, "Table 'pulse.helloservice_customer' doesn't exist")
Use Meta class and set table name. Then the table name only set customer. It does not use prefix(app name). Then make migrations and set migrate.
class Customer(models.Model):
customer_email = models.EmailField()
password = models.CharField(max_length=20)
ccustomer_first_name = models.CharField(max_length=30)
customer_last_name = models.CharField(max_length=30)
class Meta:
db_table = "customer"

django CommandError: One or more models did not validate:

The Model class are generated by the django inspectdb and i can see the Id field, this is converted from the Legacy tables, the table cant be altered here,
class workFlowTemplate(models.Model):
rev = models.IntegerField(null=True, blank=True)
revtype = models.IntegerField(null=True, blank=True)
**id = models.IntegerField(null=True, blank=True)**
name = models.CharField(max_length=255, blank=True,primary_key=False)
class Meta:
db_table = 'workflow_template'
the problem is when i try to run the django it throws error
CommandError: One or more models did not validate:
pHApp.workflowtemplate: "id": You can't use "id" as a field name, because each model automatically gets an "id" field if none of the fields have primary_key=True. You need to either remove/rename your "id" field or add primary_key=True to a field.
Note
the id is not primary key here in this case,
When i tries to change
id = models.IntegerField(null=True, blank=True,primary_key=False)
the Issue still remains the same, and same error is repeated again, how to avoid this Issue ?
A model must have a primary key, so you need specify a primary key field, like this:
my_primary_key = models.AutoField(primary_key=True)
id = models.IntegerField(null=True, blank=True)
then it won’t add the automatic id column and you will not receive this error.
Column id is adding by django by default, if you did not specified another field as PK.
In your case, you specified id as your field, but you did not specified what field should be a primary key.
Solution - set another field as primary key since you can not rename id

Django many to many admin with custom through object

lets assume that I have such a model:
from django.db import models
class Person(models.Model):
name = models.CharField(max_length=128)
class Group(models.Model):
name = models.CharField(max_length=128)
members = models.ManyToManyField(Person, through='Membership')
class Membership(models.Model):
person = models.ForeignKey(Person)
group = models.ForeignKey(Group)
date_joined = models.DateField()
invite_reason = models.CharField(max_length=64)
class Meta:
db_table = mysql_membership
So my in-the-middle table is chosen (and manually created) by me. And now I would like to create the admin with inline formset in for Group objects. Just like this:
class MembershipInline(admin.TabularInline):
model = Membership
extra = 1
class GroupAdmin(admin.ModelAdmin):
inlines = (MembershipInline,)
admin.site.register(Group, GroupAdmin)
The problem is, that when I try to get edit form for a given Group, I get this error:
Exception Type: OperationalError
Exception Value:
(1054, "Unknown column 'mysql_membership.id' in 'field list'")
This is understandable to me because the primary key in this mysql_membership table is a set of columns (instead of one simple primary_key column):
PRIMARY KEY (person, group, invite_reason)
I want you to know, that I CAN'T edit the database schema. How can I enable formset for many-to-many field based on custom "through" table/model? Please help.
btw. I was using example from:
https://docs.djangoproject.com/en/1.6/ref/contrib/admin/#working-with-many-to-many-intermediary-models

Categories

Resources