DatabaseError: column does not exist - python

I added New Column FK_Project in the rw_item Table, which is reference of the Project_Master table.
dp=# ALTER TABLE digital_publisher_rw_item ADD FK_Project int;
ALTER TABLE
dp=# ALTER TABLE digital_publisher_rw_item ADD FOREIGN KEY(FK_Project) REFERENCES digital_publisher_project_master(id);
ALTER TABLE
Now I am writing script to add values of to Column FK_Project in the rw_item Table.
But getting following exception,
Exception:
>>> rw_item
<class 'dp.digital_publisher.models.rw_item'>
>>> rw_item.objects.all()
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/lib/python2.6/site-packages/django/db/models/query.py", line 67, in __repr__
data = list(self[:REPR_OUTPUT_SIZE + 1])
File "/usr/lib/python2.6/site-packages/django/db/models/query.py", line 82, in __len__
self._result_cache.extend(self._iter)
File "/usr/lib/python2.6/site-packages/django/db/models/query.py", line 271, in iterator
for row in compiler.results_iter():
File "/usr/lib/python2.6/site-packages/django/db/models/sql/compiler.py", line 677, in results_iter
for rows in self.execute_sql(MULTI):
File "/usr/lib/python2.6/site-packages/django/db/models/sql/compiler.py", line 732, in execute_sql
cursor.execute(sql, params)
File "/usr/lib/python2.6/site-packages/django/db/backends/util.py", line 15, in execute
return self.cursor.execute(sql, params)
File "/usr/lib/python2.6/site-packages/django/db/backends/postgresql_psycopg2/base.py", line 44, in execute
return self.cursor.execute(query, args)
DatabaseError: column digital_publisher_rw_item.FK_Project_id does not exist
LINE 1: ..."digital_publisher_rw_item"."deletion_date_time", "digital_p...
^
Django models.py:
FK_Project = models.ForeignKey(Project_Master, null=True, blank=True)
How to handle this?

Since you are adding fields manually, instead of using Django's database migrations, as noted by Shang Wang, you just need to make sure that the field names match up. In this case, take note of the exact error given by Django:
DatabaseError: column digital_publisher_rw_item.FK_Project_id does not exist
It is looking for FK_Project_id, as opposed to the FK_Project, which you added. This is because of a convention in Django, which appends _id to the end of ForeignKey columns. So, you have two options for handling this:
Rename the column in the database: ALTER TABLE digital_publisher_rw_item RENAME COLUMN FK_Project TO FK_Project_id
Tell Django to use the column you created: FK_Project = models.ForeignKey(Project_Master, null=True, blank=True, db_column='FK_Project')

Related

Python. SQL Alchemy NotImplementedError: Operator 'getitem' is not supported on this expression

I create record in DB through sql alchemy. The next step is trying to capture the id of the created object into another table that has fk on the created object (assign_resource_to_user) . This is my code:
from dev_tools.models.user import User
from dev_tools.models.resource Resource
resource = Resource(
code=self.message_body['code'],
description=self.message_body['description'],
crew_id=None,
catalog_resource_type_id=self.message_body['catalog_resource_type_id'],
catalog_resource_status_id=self.message_body['catalog_resource_status_id'],
created_at=datetime.utcnow(),
created_by=None,
is_available=self.message_body['is_available'],
)
self.db_session.add(resource)
self.db_session.flush()
assign_resource_to_user = self.db_session.query(
User
).filter(
User.id == self.user_info.id
).update(
User.resource_id == resource.id
)
self.db_session.add(assign_resource_to_user)
I have error:
Traceback (most recent call last):
File "/home/Документы/project/ResourseManagment/rm-private-application-server/apps/controller/helpers/data_processing/action/custom/resource_from_a_user.py", line 227, in <module>
resources.create_record_in_db()
File "/home/Документы/project/ResourseManagment/rm-private-application-server/apps/controller/helpers/data_processing/action/custom/resource_from_a_user.py", line 211, in create_record_in_db
User.resource_id == resource.id
File "/home/Документы/project/venv/lib/python3.7/site-packages/SQLAlchemy-1.2.14-py3.7-linux-x86_64.egg/sqlalchemy/orm/query.py", line 3486, in update
File "/home/Документы/project/venv/lib/python3.7/site-packages/SQLAlchemy-1.2.14-py3.7-linux-x86_64.egg/sqlalchemy/orm/persistence.py", line 1334, in exec_
File "/home/Документы/project/venv/lib/python3.7/site-packages/SQLAlchemy-1.2.14-py3.7-linux-x86_64.egg/sqlalchemy/orm/persistence.py", line 1405, in _do_pre_synchronize
File "/home/Документы/project/venv/lib/python3.7/site-packages/SQLAlchemy-1.2.14-py3.7-linux-x86_64.egg/sqlalchemy/orm/persistence.py", line 1540, in _additional_evaluators
File "/home/Документы/project/venv/lib/python3.7/site-packages/SQLAlchemy-1.2.14-py3.7-linux-x86_64.egg/sqlalchemy/orm/persistence.py", line 1473, in _resolved_values_keys_as_propnames
File "/home/Документы/project/venv/lib/python3.7/site-packages/SQLAlchemy-1.2.14-py3.7-linux-x86_64.egg/sqlalchemy/orm/persistence.py", line 1457, in _resolved_values
File "/home/Документы/project/venv/lib/python3.7/site-packages/SQLAlchemy-1.2.14-py3.7-linux-x86_64.egg/sqlalchemy/sql/operators.py", line 411, in __getitem__
File "/home/Документы/project/venv/lib/python3.7/site-packages/SQLAlchemy-1.2.14-py3.7-linux-x86_64.egg/sqlalchemy/sql/elements.py", line 694, in operate
File "/home/Документы/project/venv/lib/python3.7/site-packages/SQLAlchemy-1.2.14-py3.7-linux-x86_64.egg/sqlalchemy/sql/operators.py", line 411, in __getitem__
File "<string>", line 1, in <lambda>
File "/home/Документы/project/venv/lib/python3.7/site-packages/SQLAlchemy-1.2.14-py3.7-linux-x86_64.egg/sqlalchemy/sql/type_api.py", line 63, in operate
File "/home/Документы/project/venv/lib/python3.7/site-packages/SQLAlchemy-1.2.14-py3.7-linux-x86_64.egg/sqlalchemy/sql/default_comparator.py", line 192, in _getitem_impl
File "/home/Документы/project/venv/lib/python3.7/site-packages/SQLAlchemy-1.2.14-py3.7-linux-x86_64.egg/sqlalchemy/sql/default_comparator.py", line 197, in _unsupported_impl
NotImplementedError: Operator 'getitem' is not supported on this expression
Error in this string:
).update(
User.resource_id == resource.id
)
Maybe someone know how fix it or have same problem and can help me solve it. Thanks.
Query.update() expects a mapping of attributes to values as the first positional argument, but you've passed it an SQL expression language object. Note that Query.update() is a bulk operation and does not return a model instance or such, but the number of matched rows. So instead do:
self.db_session.query(
User
).filter(
User.id == self.user_info.id
).update(
{User.resource_id: resource.id},
synchronize_session=False # Change this according to your needs
)
# No add

can't use pony orm on sqlite3 blob fields

Just trying some basic exercises with pony ORM (and python3.5, sqlite3).
I just want to print a select query of some data I have without further processing to start with. Pony orm does not seem to like that at all....
The sqlite db dump
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE sums (t text, path BLOB, name BLOB, sum text, primary key (path,name));
INSERT INTO "sums" VALUES('directory','','','');
INSERT INTO "sums" VALUES('file','','sums-backup-f.db','6859b35f9f026317c5df48932f9f2a91');
INSERT INTO "sums" VALUES('file','','md5-tree.py','c7af81d4aad9d00e88db7af950c264c2');
INSERT INTO "sums" VALUES('file','','test.db','a403e9b46e54d6ece851881a895b1953');
INSERT INTO "sums" VALUES('file','','sirius-alexa.db','22a20434cec550a83c675acd849002fa');
INSERT INTO "sums" VALUES('file','','sums-reseau-y.db','1021614f692b5d7bdeef2a45b6b1af5b');
INSERT INTO "sums" VALUES('file','','.md5-tree.py.swp','1c3c195b679e99ef18b3d46044f6e6c5');
INSERT INTO "sums" VALUES('file','','compare-md5.py','cfb4a5b3c7c4e62346aa5e1affef210a');
INSERT INTO "sums" VALUES('file','','charles.local.db','9c50689e8185e5a79fd9077c14636405');
COMMIT;
Here is the code I try to run on python3.5 interactive shell:
from pony.orm import *
db = Database()
class File(db.Entity) :
_table_ = 'sums'
t = Required(str)
path = Required(bytes)
name = Required(bytes)
sum = Required(str)
PrimaryKey(path,name)
db.bind('sqlite','/some/edited/path/test.db')
db.generate_mapping()
File.select().show()
And it fails like this :
Traceback (most recent call last):
File "/usr/lib/python3.5/site-packages/pony/orm/core.py", line 5149, in _fetch
try: result = cache.query_results[query_key]
KeyError: (('f', 0, ()), (<pony.orm.ormtypes.SetType object at 0x7fd2d2701708>,), False, None, None, None, False, False, False, ())
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 2, in show
File "/usr/lib/python3.5/site-packages/pony/utils/utils.py", line 75, in cut_traceback
raise exc # Set "pony.options.CUT_TRACEBACK = False" to see full traceback
File "/usr/lib/python3.5/site-packages/pony/utils/utils.py", line 60, in cut_traceback
try: return func(*args, **kwargs)
File "/usr/lib/python3.5/site-packages/pony/orm/core.py", line 5256, in show
query._fetch().show(width)
File "/usr/lib/python3.5/site-packages/pony/orm/core.py", line 5155, in _fetch
used_attrs=translator.get_used_attrs())
File "/usr/lib/python3.5/site-packages/pony/orm/core.py", line 3859, in _fetch_objects
real_entity_subclass, pkval, avdict = entity._parse_row_(row, attr_offsets)
File "/usr/lib/python3.5/site-packages/pony/orm/core.py", line 3889, in _parse_row_
avdict[attr] = attr.parse_value(row, offsets)
File "/usr/lib/python3.5/site-packages/pony/orm/core.py", line 1922, in parse_value
val = attr.validate(row[offset], None, attr.entity, from_db=True)
File "/usr/lib/python3.5/site-packages/pony/orm/core.py", line 2218, in validate
val = Attribute.validate(attr, val, obj, entity, from_db)
File "/usr/lib/python3.5/site-packages/pony/orm/core.py", line 1894, in validate
if from_db: return converter.sql2py(val)
File "/usr/lib/python3.5/site-packages/pony/orm/dbapiprovider.py", line 619, in sql2py
if not isinstance(val, buffer): val = buffer(val)
TypeError: string argument without an encoding
Am I using this wrong, or is this a bug ? I don't mind go filing a bug, but it's the first time I'm using this orm, so I thought it might be better to check first ...
SQLite has a (mis)feature, which allows a column to store an arbitrary value disregarding the column type. Instead of rigid data type, each SQLite column has an affinity, while each value has a storage class which can be different within the same column. For example, you can store text value inside an integer column, and vice versa. See Datatypes In SQLite Version 3 for more information.
The reason for the error is that the table contains values of "wrong" type in its BLOB columns. Correct SQLite binary literal looks like x'abcdef'. The INSERT commands that you use insert UTF8 strings instead.
This problem was somewhat fixed in the latest version of Pony which you can take from GitHub. Now if Pony receives a string value from a BLOB column it just keep that value without throwing an exception.
If you populate the table with Pony, it will writes BLOB data as a correct binary values, so it can read them later without any problem.

FieldError: Invalid field name(s) given in select_related: 'userinfo'. Choices are: userinfo

I get this error when trying to use only with select_related.
FieldError: Invalid field name(s) given in select_related: 'userinfo'. Choices are: userinfo
It's a little strange that it reports the field I'm trying to select as an error. Here is my query:
users_with_schools = User.objects.select_related('userinfo').only(
"id",
"date_joined",
"userinfo__last_coordinates_id",
"userinfo__school_id"
).filter(
userinfo__school_id__isnull=False,
date_joined__gte=start_date
)
I've been able to use select_related with only in other places in my code so I am not sure why this is happening.
Edit: Here is the full traceback
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "env/lib/python2.7/site-packages/django/db/models/query.py", line 138, in __repr__
data = list(self[:REPR_OUTPUT_SIZE + 1])
File "env/lib/python2.7/site-packages/django/db/models/query.py", line 162, in __iter__
self._fetch_all()
File "env/lib/python2.7/site-packages/django/db/models/query.py", line 965, in _fetch_all
self._result_cache = list(self.iterator())
File "env/lib/python2.7/site-packages/django/db/models/query.py", line 238, in iterator
results = compiler.execute_sql()
File "env/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 818, in execute_sql
sql, params = self.as_sql()
File "env/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 367, in as_sql
extra_select, order_by, group_by = self.pre_sql_setup()
File "env/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 48, in pre_sql_setup
self.setup_query()
File "env/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 39, in setup_query
self.select, self.klass_info, self.annotation_col_map = self.get_select()
File "env/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 203, in get_select
related_klass_infos = self.get_related_selections(select)
File "env/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 743, in get_related_selections
', '.join(_get_field_choices()) or '(none)',
FieldError: Invalid field name(s) given in select_related: 'userinfo'. Choices are: userinfo
From the documentation:
All of the cautions in the note for the defer() documentation apply to only() as well. Use it cautiously and only after exhausting your other options.
...
Using only() and omitting a field requested using select_related() is an error as well.
select_related will try to fetch all the columns for userinfo. As described above, trying to restrict the set of columns to specific ones will result in an error - the combination of select_related and only does not support that.
It's probably worth noting the comment that goes with these methods:
The defer() method (and its cousin, only(), below) are only for advanced use-cases. They provide an optimization for when you have analyzed your queries closely and understand exactly what information you need and have measured that the difference between returning the fields you need and the full set of fields for the model will be significant.
Edit: you mention in a comment below that the following query, used elsewhere in your code, seems to work fine:
ChatUser.objects.select_related("user__userinfo").\
only( "id", "chat_id", "user__id", "user__username", "user__userinfo__id" )
My best guess is that you are hitting this bug in Django (fixed in 1.10).
I suppose the easiest way to verify is to check the SQL query generated by the queryset that seems to work. My guess is that you will find that it isn't actually querying everything in one go and that there are additional queries when you try to access the related model that you asked to be fetched in select_related.

Django - Error selecting objects: "ProgrammingError: operator does not exist: character varying = integer)

My models.py contains
class Patient(models.Model):
cpf_id = models.CharField(max_length=15, unique=True)
name_txt = models.CharField(max_length=50)
nr_record = models.AutoField(primary_key=True)
The database is postgresql.
When I try to select an object with
>>> Patient.objects.get(nr_record=1)
I get the error
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/django/db/models/manager.py", line 151, in get
return self.get_queryset().get(*args, **kwargs)
.
.
.
File "/usr/local/lib/python2.7/dist-packages/django/db/backends/util.py", line 53, in execute
return self.cursor.execute(sql, params)
ProgrammingError: operator does not exist: character varying = integer
LINE 1: ...d" FROM "quiz_patient" WHERE "quiz_patient"."nr_record" = 1
^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts.
I'm newbie in Django-python. But until I know models.Autofield is integer type.
Searched web, but despite posts with same error message, didn't find nothing like my problem.
The problem is most certainly with your database. Maybe you had a previous version of the model where nr_record was not an integer?
Try removing the nr_record column in the database and adding it again.

changing django default model settings

I'm just starting with the django creating your own app tutorial (creating a Poll) I'm deviating slightly as I'm wanting to create an app using my own database model that already exists.
And in the tutorial it says
Table names are automatically
generated by combining the name of
the app (polls) and the lowercase
name of the model -- poll and choice.
(You can override this behavior.)
Primary keys (IDs) are added
automatically. (You can override
this, too.)
By convention, Django appends
"_id" to the foreign key field
name. Yes, you can override this,
as well.
But I can't see where it mentions how you can override this behaviour? I've defined my model as so
from django.db import models
# Create your models here.
class Channels(models.Model):
channelid = models.IntegerField()
channelid.primary_key = True
channelname = models.CharField(max_length=50)
Now when I go in to the shell this is what I get
>>> from tvlistings.models import *
>>> Channels.objects.all()
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "C:\Python26\lib\site-packages\django\db\models\query.py", line 67, in __
repr__
data = list(self[:REPR_OUTPUT_SIZE + 1])
File "C:\Python26\lib\site-packages\django\db\models\query.py", line 82, in __
len__
self._result_cache.extend(list(self._iter))
File "C:\Python26\lib\site-packages\django\db\models\query.py", line 271, in i
terator
for row in compiler.results_iter():
File "C:\Python26\lib\site-packages\django\db\models\sql\compiler.py", line 67
7, in results_iter
for rows in self.execute_sql(MULTI):
File "C:\Python26\lib\site-packages\django\db\models\sql\compiler.py", line 73
2, in execute_sql
cursor.execute(sql, params)
File "C:\Python26\lib\site-packages\django\db\backends\util.py", line 15, in e
xecute
return self.cursor.execute(sql, params)
File "C:\Python26\lib\site-packages\django\db\backends\mysql\base.py", line 86
, in execute
return self.cursor.execute(query, args)
File "C:\Python26\lib\site-packages\MySQLdb\cursors.py", line 166, in execute
self.errorhandler(self, exc, value)
File "C:\Python26\lib\site-packages\MySQLdb\connections.py", line 35, in defau
lterrorhandler
raise errorclass, errorvalue
DatabaseError: (1146, "Table 'tvlistings.tvlistings_channels' doesn't exist")
Obviously it can't find the table tvlistings_channels as it's actually called channels. So how do I change the default?
You can override Model behavior in Django largely through the use of an inner Meta class
db_table allows you to rename the table name
specifying another field as the primary key will have it use that rather than a surrogate key (not in the Meta class, just in the model itself)
You should try and work your way all through the tutorial before you try and customise things. All these things are covered in the actual documentation, but it's best to have a basic understanding of things first before diving into that.
FWIW, here are the docs on defining your own primary key and specifying a table name. But really, do the tutorial as written first.

Categories

Resources