Django TestCase fails to load fixture data with MySQL - python

I am trying to create tests to check a newly implemented login process, however I got stuck at the very beginning.
When I try to make a query in my tests, Django throws a MySQLdb._exceptions.OperationalError and fails to continue. I noticed, that this issue happens only when I use django.test.TestCase, but not when I use django.test.TransactionTestCase. Now I read that there is a difference between how these two handle transactions, but I still cannot understand why one fails when the other one works. Can someone please point me in the right direction?
This code fails:
class AuthenticateUserTest(TestCase):
fixtures = ["test_users.json"]
serialized_rollback = True
def test_login_success(self):
print(User.objects.all())
but this code doesn't:
class AuthenticateUserTest(TransactionTestCase):
fixtures = ["test_users.json"]
serialized_rollback = True
def test_login_success(self):
print(User.objects.all())
with this fixture:
[
{
"model": "app.user",
"pk": 1,
"fields": {
"created_at": "2022-04-07T13:34:53+01:00",
"email": "test#test.com",
"password": "topsecret",
"is_active": true,
"roles": "[\"SUPER_ADMIN\"]",
"first_name": "Test",
"last_name": "User"
}
}
]
and with the following error:
ERROR: test_login_success (tests.auth.test_auth_api.AuthenticateUserTest)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/app/tests/auth/test_auth_api.py", line 36, in test_login_success
print(User.objects.all())
File "/usr/local/lib/python3.8/site-packages/django/db/models/query.py", line 252, in __repr__
data = list(self[:REPR_OUTPUT_SIZE + 1])
File "/usr/local/lib/python3.8/site-packages/django/db/models/query.py", line 258, in __len__
self._fetch_all()
File "/usr/local/lib/python3.8/site-packages/django/db/models/query.py", line 1261, in _fetch_all
self._result_cache = list(self._iterable_class(self))
File "/usr/local/lib/python3.8/site-packages/django/db/models/query.py", line 57, in __iter__
results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
File "/usr/local/lib/python3.8/site-packages/django/db/models/sql/compiler.py", line 1155, in execute_sql
cursor.close()
File "/usr/local/lib/python3.8/site-packages/MySQLdb/cursors.py", line 83, in close
while self.nextset():
File "/usr/local/lib/python3.8/site-packages/MySQLdb/cursors.py", line 137, in nextset
nr = db.next_result()
MySQLdb._exceptions.OperationalError: (2006, '')

Related

The transaction declared chain ID 5777, but the connected node is on 1337

I am trying to deploy my SimpleStorage.sol contract to a ganache local chain by making a transaction using python. It seems to have trouble connecting to the chain.
from solcx import compile_standard
from web3 import Web3
import json
import os
from dotenv import load_dotenv
load_dotenv()
with open("./SimpleStorage.sol", "r") as file:
simple_storage_file = file.read()
compiled_sol = compile_standard(
{
"language": "Solidity",
"sources": {"SimpleStorage.sol": {"content": simple_storage_file}},
"settings": {
"outputSelection": {
"*": {"*": ["abi", "metadata", "evm.bytecode", "evm.sourceMap"]}
}
},
},
solc_version="0.6.0",
)
with open("compiled_code.json", "w") as file:
json.dump(compiled_sol, file)
# get bytecode
bytecode = compiled_sol["contracts"]["SimpleStorage.sol"]["SimpleStorage"]["evm"][
"bytecode"
]["object"]
# get ABI
abi = compiled_sol["contracts"]["SimpleStorage.sol"]["SimpleStorage"]["abi"]
# to connect to ganache blockchain
w3 = Web3(Web3.HTTPProvider("HTTP://127.0.0.1:7545"))
chain_id = 5777
my_address = "0xca1EA31e644F13E3E36631382686fD471c62267A"
private_key = os.getenv("PRIVATE_KEY")
# create the contract in python
SimpleStorage = w3.eth.contract(abi=abi, bytecode=bytecode)
# get the latest transaction
nonce = w3.eth.getTransactionCount(my_address)
# 1. Build a transaction
# 2. Sign a transaction
# 3. Send a transaction
transaction = SimpleStorage.constructor().buildTransaction(
{"chainId": chain_id, "from": my_address, "nonce": nonce}
)
print(transaction)
It seems to be connected to the ganache chain because it prints the nonce, but when I build and try to print the transaction
here is the entire traceback call I am receiving
Traceback (most recent call last):
File "C:\Users\evens\demos\web3_py_simple_storage\deploy.py", line
52, in <module>
transaction = SimpleStorage.constructor().buildTransaction(
File "C:\Python310\lib\site-packages\eth_utils\decorators.py", line
18, in _wrapper
return self.method(obj, *args, **kwargs)
File "C:\Users\evens\AppData\Roaming\Python\Python310\site-
packages\web3\contract.py", line 684, in buildTransaction
return fill_transaction_defaults(self.web3, built_transaction)
File "cytoolz/functoolz.pyx", line 250, in
cytoolz.functoolz.curry.__call__
return self.func(*args, **kwargs)
File "C:\Users\evens\AppData\Roaming\Python\Python310\site-
packages\web3\_utils\transactions.py", line 114, in
fill_transaction_defaults
default_val = default_getter(web3, transaction)
File "C:\Users\evens\AppData\Roaming\Python\Python310\site-
packages\web3\_utils\transactions.py", line 60, in <lambda>
'gas': lambda web3, tx: web3.eth.estimate_gas(tx),
File "C:\Users\evens\AppData\Roaming\Python\Python310\site-
packages\web3\eth.py", line 820, in estimate_gas
return self._estimate_gas(transaction, block_identifier)
File "C:\Users\evens\AppData\Roaming\Python\Python310\site-
packages\web3\module.py", line 57, in caller
result = w3.manager.request_blocking(method_str,
File "C:\Users\evens\AppData\Roaming\Python\Python310\site-
packages\web3\manager.py", line 197, in request_blocking
response = self._make_request(method, params)
File "C:\Users\evens\AppData\Roaming\Python\Python310\site-
packages\web3\manager.py", line 150, in _make_request
return request_func(method, params)
File "cytoolz/functoolz.pyx", line 250, in
cytoolz.functoolz.curry.__call__
return self.func(*args, **kwargs)
File "C:\Users\evens\AppData\Roaming\Python\Python310\site-
packages\web3\middleware\formatting.py", line 76, in
apply_formatters
response = make_request(method, params)
File "C:\Users\evens\AppData\Roaming\Python\Python310\site-
packages\web3\middleware\gas_price_strategy.py", line 90, in
middleware
return make_request(method, params)
File "cytoolz/functoolz.pyx", line 250, in
cytoolz.functoolz.curry.__call__
return self.func(*args, **kwargs)
File "C:\Users\evens\AppData\Roaming\Python\Python310\site-
packages\web3\middleware\formatting.py", line 74, in
apply_formatters
response = make_request(method, formatted_params)
File "C:\Users\evens\AppData\Roaming\Python\Python310\site-
packages\web3\middleware\attrdict.py", line 33, in middleware
response = make_request(method, params)
File "cytoolz/functoolz.pyx", line 250, in
cytoolz.functoolz.curry.__call__
return self.func(*args, **kwargs)
File "C:\Users\evens\AppData\Roaming\Python\Python310\site-
packages\web3\middleware\formatting.py", line 74, in
apply_formatters
response = make_request(method, formatted_params)
File "cytoolz/functoolz.pyx", line 250, in
cytoolz.functoolz.curry.__call__
return self.func(*args, **kwargs)
File "C:\Users\evens\AppData\Roaming\Python\Python310\site-
packages\web3\middleware\formatting.py", line 73, in
apply_formatters
formatted_params = formatter(params)
File "cytoolz/functoolz.pyx", line 503, in
cytoolz.functoolz.Compose.__call__
ret = PyObject_Call(self.first, args, kwargs)
File "cytoolz/functoolz.pyx", line 250, in
cytoolz.functoolz.curry.__call__
return self.func(*args, **kwargs)
File "C:\Python310\lib\site-packages\eth_utils\decorators.py", line
91, in wrapper
return ReturnType(result) # type: ignore
File "C:\Python310\lib\site-packages\eth_utils\applicators.py", line
22, in apply_formatter_at_index
yield formatter(item)
File "cytoolz/functoolz.pyx", line 250, in
cytoolz.functoolz.curry.__call__
File "cytoolz/functoolz.pyx", line 250, in
cytoolz.functoolz.curry.__call__
return self.func(*args, **kwargs)
File "C:\Python310\lib\site-packages\eth_utils\applicators.py", line
72, in apply_formatter_if
return formatter(value)
File "cytoolz/functoolz.pyx", line 250, in
cytoolz.functoolz.curry.__call__
return self.func(*args, **kwargs)
File "C:\Users\evens\AppData\Roaming\Python\Python310\site-
packages\web3\middleware\validation.py", line 57, in
validate_chain_id
raise ValidationError(
web3.exceptions.ValidationError: The transaction declared chain ID
5777, but the connected node is on 1337
Had this issue myself, apparently it's some sort of Ganache CLI error but the simplest fix I could find was to change the network id in Ganache through settings>server to 1337. It restarts the session so you'd then need to change the address and private key variable.
If it's the same tutorial I'm doing, you're likely to come unstuck after this... the code for transaction should be:
transaction =
SimpleStorage.constructor().buildTransaction( {
"gasPrice": w3.eth.gas_price,
"chainId": chain_id,
"from": my_address,
"nonce": nonce,
})
print(transaction)
Otherwise you get a value error if you don't set the gasPrice
this line of code is wrong
chain_id = 5777
Ganache chain id is not 5777. This is network id. Network id is used by nodes to transfer data between nodes that are on the same network. Network id is not included in blocks and it is not used for signing transactions or mining blocks.
chain_id = 1377
Chain ID is not included in blocks either, but it is used during the transaction signing and verification process.
It works for me, I get value chain_id from w3.eth.chain_id
transaction = SimpleStorage.constructor().buildTransaction(
{
"gasPrice": w3.eth.gas_price,
"chainId": w3.eth.chain_id,
"from": my_address,
"nonce": nonce,
}
)
Delegates to eth_chainId RPC Method
Returns an integer value for the currently configured “Chain Id” value introduced in EIP-155. Returns None if no Chain Id is available.
Hey it happened to me too, you need to build the constructor like this
SimpleStorage.constructor().buildTransaction( {
"gasPrice": w3.eth.gas_price,
"chainId": chain_id,
"from": my_address,
"nonce": nonce,
You need to add the gas price part.
Worked for me
I encountered this issue today and after debugging, I am certain that the issue is from the dotenv config variables imported. All the variables are been seen by python as strings, and for some reason, the web3 library swaps the value 5777 if the type of your chain id is not an integer.
The perfect fix is to cast your chain_id into type int before deploying your smart contract.
Hope this saves y'all some pondering time, peace out!
Snippet example below:
transaction = SimpleStorage.constructor().buildTransaction(
{
"gasPrice": w3.eth.gas_price,
"chainId": int(chain_id),
"from": my_address,
"nonce": nonce,
}
)
Perhaps, you are following FreeCodeCamp tutorial.
I have got the same problem
In my case, it is working after adding gas price, "gasPrice": w3.eth.gas_price
transaction = SimpleStorage.constructor().buildTransaction({ "gasPrice": w3.eth.gas_price, "chainId" : chain_id, "from": my_address, "nonce": nonce})
print(transaction)
You can check your chainId using the following code
const chainId = await wallet.getChainId();
console.log("chain Id",chainId);
For me it was 1337

Keep jsonschema from always making requests to URI

Background
I am trying to validate a JSON file using jsonchema. However, the library is trying to make a GET request and I want to avoid that.
from jsonschema import validate
point_schema = {
"$id": "https://example.com/schemas/point",
"type": "object",
"properties": {"x": {"type": "number"}, "y": {"type": "number"}},
"required": ["x", "y"],
}
polygon_schema = {
"$id": "https://example.com/schemas/polygon",
"type": "array",
"items": {"$ref": "https://example.com/schemas/point"},
}
a_polygon = [{'x': 1, 'y': 2}, {'x': 3, 'y': 4}, {'x': 1, 'y': 2}]
validate(instance=a_polygon, schema=polygon_schema)
Error
I am trying to connect both schemas using a $ref key from the spec:
https://json-schema.org/understanding-json-schema/structuring.html?highlight=ref#ref
Unfortunately for me, this means the library will make a GET request to the URI specified and try to decode it:
Traceback (most recent call last):
File "/home/user/anaconda3/envs/myapp-py/lib/python3.7/site-packages/jsonschema/validators.py", line 777, in resolve_from_url
document = self.resolve_remote(url)
File "/home/user/anaconda3/envs/myapp-py/lib/python3.7/site-packages/jsonschema/validators.py", line 860, in resolve_remote
result = requests.get(uri).json()
File "/home/user/anaconda3/envs/myapp-py/lib/python3.7/site-packages/requests/models.py", line 910, in json
return complexjson.loads(self.text, **kwargs)
File "/home/user/anaconda3/envs/myapp-py/lib/python3.7/json/__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "/home/user/anaconda3/envs/myapp-py/lib/python3.7/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/home/user/anaconda3/envs/myapp-py/lib/python3.7/json/decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/user/anaconda3/envs/myapp-py/lib/python3.7/site-packages/jsonschema/validators.py", line 932, in validate
error = exceptions.best_match(validator.iter_errors(instance))
File "/home/user/anaconda3/envs/myapp-py/lib/python3.7/site-packages/jsonschema/exceptions.py", line 367, in best_match
best = next(errors, None)
File "/home/user/anaconda3/envs/myapp-py/lib/python3.7/site-packages/jsonschema/validators.py", line 328, in iter_errors
for error in errors:
File "/home/user/anaconda3/envs/myapp-py/lib/python3.7/site-packages/jsonschema/_validators.py", line 81, in items
for error in validator.descend(item, items, path=index):
File "/home/user/anaconda3/envs/myapp-py/lib/python3.7/site-packages/jsonschema/validators.py", line 344, in descend
for error in self.iter_errors(instance, schema):
File "/home/user/anaconda3/envs/myapp-py/lib/python3.7/site-packages/jsonschema/validators.py", line 328, in iter_errors
for error in errors:
File "/home/user/anaconda3/envs/myapp-py/lib/python3.7/site-packages/jsonschema/_validators.py", line 259, in ref
scope, resolved = validator.resolver.resolve(ref)
File "/home/user/anaconda3/envs/myapp-py/lib/python3.7/site-packages/jsonschema/validators.py", line 766, in resolve
return url, self._remote_cache(url)
File "/home/user/anaconda3/envs/myapp-py/lib/python3.7/site-packages/jsonschema/validators.py", line 779, in resolve_from_url
raise exceptions.RefResolutionError(exc)
jsonschema.exceptions.RefResolutionError: Expecting value: line 1 column 1 (char 0)
I don't want this, I just want the polygon schema to reference the point schema that is right above (as for this purpose, a polygon is a list of points).
In fact, these schemas are in the same file.
Questions
I could always do the following:
point_schema = {
"$id": "https://example.com/schemas/point",
"type": "object",
"properties": {"x": {"type": "number"}, "y": {"type": "number"}},
"required": ["x", "y"],
}
polygon_schema = {
"$id": "https://example.com/schemas/polygon",
"type": "array",
"items": point_schema,
}
And this would technically work.
However I would simply be build a bigger dictionary and I would not be using the spec as it was designed to.
How can I use the spec to solve my problem?
You have to provide your other schemas to the implementation.
With this implementation, you must provide a RefResolver to the validate function.
You'll need to either provide a single base_uri and referrer (the schema), or a store which contains a dictionary of URI to schema.
Additionally, you may handle protocols with a function.
Your RefResolver would look like the following...
refResolver = jsonschema.RefResolver(referrer=point_schema, base_uri='https://example.com/schemas/point'

jsonb join not working properly in sqlalchemy

I have a query that joins on a jsonb type column in postgres that I want to convert to sqlalchemy in django using the aldjemy package
SELECT anon_1.key AS tag, count(anon_1.value ->> 'polarity') AS count_1, anon_1.value ->> 'polarity' AS anon_2
FROM feedback f
JOIN tagging t ON t.feedback_id = f.id
JOIN jsonb_each(t.json_content -> 'entityMap') AS anon_3 ON true
JOIN jsonb_each(((anon_3.value -> 'data') - 'selectionState') - 'segment') AS anon_1 ON true
where f.id = 2
GROUP BY anon_1.value ->> 'polarity', anon_1.key;
The json_content field stores data in the following format:
{
"entityMap":
{
"0":
{
"data":
{
"people":
{
"labelId": 5,
"polarity": "positive"
},
"segment": "a small segment",
"selectionState":
{
"focusKey": "9xrre",
"hasFocus": true,
"anchorKey": "9xrre",
"isBackward": false,
"focusOffset": 75,
"anchorOffset": 3
}
},
"type": "TAG",
"mutability": "IMMUTABLE"
},
"1":
{
"data":
{
"product":
{
"labelId": 6,
"polarity": "positive"
},
"segment": "another segment",
"selectionState":
{
"focusKey": "9xrre",
"hasFocus": true,
"anchorKey": "9xrre",
"isBackward": false,
"focusOffset": 138,
"anchorOffset": 79
}
},
"type": "TAG",
"mutability": "IMMUTABLE"
}
}
}
I wrote the following sqlalchemy code to achieve the query
first_alias = aliased(func.jsonb_each(Tagging.sa.json_content["entityMap"]))
print(first_alias)
second_alias = aliased(
func.jsonb_each(
first_alias.c.value.op("->")("data")
.op("-")("selectionState")
.op("-")("segment")
)
)
polarity = second_alias.c.value.op("->>")("polarity")
p_tag = second_alias.c.key
_count = (
Feedback.sa.query()
.join(
CampaignQuestion,
CampaignQuestion.sa.question_id == Feedback.sa.question_id,
isouter=True,
)
.join(Tagging)
.join(first_alias, true())
.join(second_alias, true())
.filter(CampaignQuestion.sa.campaign_id == campaign_id)
.with_entities(p_tag.label("p_tag"), func.count(polarity), polarity)
.group_by(polarity, p_tag)
.all()
)
print(_count)
but it is giving me a NotImplementedError: Operator 'getitem' is not supported on this expression error on accessing first_alias.c
the stack trace:
Traceback (most recent call last):
File "/home/.cache/pypoetry/virtualenvs/api-FPSaTdE5-py3.8/lib/python3.8/site-packages/rest_framework/views.py", line 506, in dispatch
response = handler(request, *args, **kwargs)
File "/home/work/api/app/campaign/views.py", line 119, in results_p_tags
d = campaign_service.get_p_tag_count_for_campaign_results(id)
File "/home/work/api/app/campaign/services/campaign.py", line 177, in get_p_tag_count_for_campaign_results
return campaign_selectors.get_p_tag_counts_for_campaign(campaign_id)
File "/home/work/api/app/campaign/selectors.py", line 196, in get_p_tag_counts_for_campaign
polarity = second_alias.c.value.op("->>")("polarity")
File "/home/.cache/pypoetry/virtualenvs/api-FPSaTdE5-py3.8/lib/python3.8/site-packages/sqlalchemy/util/langhelpers.py", line 1093, in __get__
obj.__dict__[self.__name__] = result = self.fget(obj)
File "/home/.cache/pypoetry/virtualenvs/api-FPSaTdE5-py3.8/lib/python3.8/site-packages/sqlalchemy/sql/selectable.py", line 746, in columns
self._populate_column_collection()
File "/home/.cache/pypoetry/virtualenvs/api-FPSaTdE5-py3.8/lib/python3.8/site-packages/sqlalchemy/sql/selectable.py", line 1617, in _populate_column_collection
self.element._generate_fromclause_column_proxies(self)
File "/home/.cache/pypoetry/virtualenvs/api-FPSaTdE5-py3.8/lib/python3.8/site-packages/sqlalchemy/sql/selectable.py", line 703, in _generate_fromclause_column_proxies
fromclause._columns._populate_separate_keys(
File "/home/.cache/pypoetry/virtualenvs/api-FPSaTdE5-py3.8/lib/python3.8/site-packages/sqlalchemy/sql/base.py", line 1216, in _populate_separate_keys
self._colset.update(c for k, c in self._collection)
File "/home/.cache/pypoetry/virtualenvs/api-FPSaTdE5-py3.8/lib/python3.8/site-packages/sqlalchemy/sql/base.py", line 1216, in <genexpr>
self._colset.update(c for k, c in self._collection)
File "/home/.cache/pypoetry/virtualenvs/api-FPSaTdE5-py3.8/lib/python3.8/site-packages/sqlalchemy/sql/operators.py", line 434, in __getitem__
return self.operate(getitem, index)
File "/home/.cache/pypoetry/virtualenvs/api-FPSaTdE5-py3.8/lib/python3.8/site-packages/sqlalchemy/sql/elements.py", line 831, in operate
return op(self.comparator, *other, **kwargs)
File "/home/.cache/pypoetry/virtualenvs/api-FPSaTdE5-py3.8/lib/python3.8/site-packages/sqlalchemy/sql/operators.py", line 434, in __getitem__
return self.operate(getitem, index)
File "/home/.cache/pypoetry/virtualenvs/api-FPSaTdE5-py3.8/lib/python3.8/site-packages/sqlalchemy/sql/type_api.py", line 75, in operate
return o[0](self.expr, op, *(other + o[1:]), **kwargs)
File "/home/.cache/pypoetry/virtualenvs/api-FPSaTdE5-py3.8/lib/python3.8/site-packages/sqlalchemy/sql/default_comparator.py", line 173, in _getitem_impl
_unsupported_impl(expr, op, other, **kw)
File "/home/.cache/pypoetry/virtualenvs/api-FPSaTdE5-py3.8/lib/python3.8/site-packages/sqlalchemy/sql/default_comparator.py", line 177, in _unsupported_impl
raise NotImplementedError(
NotImplementedError: Operator 'getitem' is not supported on this expression
Any help would be greatly appreciated
PS: The sqlalchemy version I'm using for this is 1.4.6
I used the same sqlalchmy query expression before in a flask project using sqlalchemy version 1.3.22 and it was working correctly
Fixed the issue by using table_valued functions as mentioned in the docs,
and accessing the ColumnCollection of the function using indices instead of keys. Code is as follows:
first_alias = func.jsonb_each(Tagging.sa.json_content["entityMap"]).table_valued(
"key", "value"
)
second_alias = func.jsonb_each(
first_alias.c[1].op("->")("data").op("-")("selectionState").op("-")("segment")
).table_valued("key", "value")
polarity = second_alias.c[1].op("->>")("polarity")
p_tag = second_alias.c[0]

Why MongoEngine/pymongo giving error when trying to access object first time only

I have defined MongoEngine classes which are mapped with MongoDB. When I am trying to access the data using MongoEngine, at the specific code it is failing at first attempt but successfully returns data in the second attempt with the same code. Executing the code in python terminal
from Project.Mongo import User
user = User.objects(username = 'xyz#xyz.com').first()
from Project.Mongo import Asset
Asset.objects(org = user.org)
Last line from code generating the following error in first attempt.
Traceback (most recent call last):
File "", line 1, in
File "/usr/local/lib/python3.5/dist-packages/mongoengine/queryset/manager.py", line 37, in get
queryset = queryset_class(owner, owner._get_collection())
File "/usr/local/lib/python3.5/dist-packages/mongoengine/document.py", line 209, in _get_collection
cls.ensure_indexes()
File "/usr/local/lib/python3.5/dist-packages/mongoengine/document.py", line 765, in ensure_indexes
collection.create_index(fields, background=background, **opts)
File "/usr/local/lib/python3.5/dist-packages/pymongo/collection.py", line 1754, in create_index
self.__create_index(keys, kwargs, session, **cmd_options)
File "/usr/local/lib/python3.5/dist-packages/pymongo/collection.py", line 1656, in __create_index
session=session)
File "/usr/local/lib/python3.5/dist-packages/pymongo/collection.py", line 245, in _command
retryable_write=retryable_write)
File "/usr/local/lib/python3.5/dist-packages/pymongo/pool.py", line 517, in command
collation=collation)
File "/usr/local/lib/python3.5/dist-packages/pymongo/network.py", line 125, in command
parse_write_concern_error=parse_write_concern_error)
File "/usr/local/lib/python3.5/dist-packages/pymongo/helpers.py", line 145, in _check_command_response
raise OperationFailure(msg % errmsg, code, response)
pymongo.errors.OperationFailure: Index: { v: 2, key: { org: 1, _fts: "text", _ftsx: 1 }, name: "org_1_name_content_text_description_text_content_text_tag_content_text_remote.source_text", ns: "digitile.asset", weights: { content: 3, description: 1, name_content: 10, remote.owner__name: 20, remote.source: 2, tag_content: 2 }, default_language: "english", background: false, language_override: "language", textIndexVersion: 3 } already exists with different options: { v: 2, key: { org: 1, _fts: "text", _ftsx: 1 }, name: "org_1_name_text_description_text_content_text_tag_content_text_remote.source_text", ns: "digitile.asset", default_language: "english", background: false, weights: { content: 3, description: 1, name: 10, remote.owner__name: 20, remote.source: 2, tag_content: 2 }, language_override: "language", textIndexVersion: 3 }
When I try same last line second time, it produces accurate result
I am using python 3.5.2
pymongo 3.7.2
mongoengine 0.10.6
The first time you call .objects on a document class, mongoengine tries to create the indexes if they don't exist.
In this case it fails during the creation of an index on the asset collection (detail of indexes are taken from your Asset/User Document classes) as you can see in the error message:
pymongo.errors.OperationFailure: Index: {...new index details...} already exists with different options {...existing index details...}.
The second time you make that call, mongoengine assumes that the indexes were created and isn't attempting to create it again, which explains why the second call passes.

How to fix ValueError: Expecting property name: line 4 column 1 (char 43)

when I tr to run python manage.py runserver code is giving error . And its traceback is strange ,
I tried
JSON ValueError: Expecting property name: line 1 column 2 (char 1)
and all similar questions but didn't get what exactly I am facing.
Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/home/tousif/.local/lib/python2.7/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
utility.execute()
File "/home/tousif/.local/lib/python2.7/site-packages/django/core/management/__init__.py", line 308, in execute
settings.INSTALLED_APPS
File "/home/tousif/.local/lib/python2.7/site-packages/django/conf/__init__.py", line 56, in __getattr__
self._setup(name)
File "/home/tousif/.local/lib/python2.7/site-packages/django/conf/__init__.py", line 41, in _setup
self._wrapped = Settings(settings_module)
File "/home/tousif/.local/lib/python2.7/site-packages/django/conf/__init__.py", line 110, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/tousif/Desktop/ITP/ITP/itpcrm/itpcrm/settings.py", line 55, in <module>
cfg = json.loads(open('/home/tousif/Desktop/ITP/ITP/itpcrm/config.json', 'r').read())
File "/usr/lib/python2.7/json/__init__.py", line 339, in loads
return _default_decoder.decode(s)
File "/usr/lib/python2.7/json/decoder.py", line 364, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python2.7/json/decoder.py", line 380, in raw_decode
obj, end = self.scan_once(s, idx)
ValueError: Expecting property name: line 4 column 1 (char 43)
my config.json file which containt credentials etc (I have changed credentials to post here ) .And got this file from live server where its working fine but on local its giving this error.
{
"dev": {
"db": {
​
"ENGINE": "django.db.backends.mysql",
"NAME": "itpcrm",
"USER": "root",
"PASSWORD": "password",
"HOST": "localhost",
"PORT": "3306"
},
"jwt_key": "GRESDFwef3452fwefer",
"voice_api_url": "http://192.112.255.32:9040",
"voice_api_key": "3123",
"auth_api_key": "379h4f73f",
"provisioner_api_key": "abc",
"quote_approval_url": "http://192.112.255.145:9998/quotes/customer-approval?token=",
"docusign_base_url": "https://demo.docusign.net/restapi",
"docusign_integrator_key": "8a256bde-405b",
"docusign_oauth_base_url": "account-d.docusign.com",
"docusign_redirect_uri": "http://192.112.255.145:9998/api/callbacks/docusign",
"docusign_private_key_filename": "/home/itp/docusign-examples/keys/docusign_private_key.txt",
"docusign_user_id": "7f2444f-ae99-54922fec68f6",
"docusign_user_name": "dor.com"
},
"prod": {
"db": {
​
"ENGINE": "django.db.backends.mysql",
"NAME": "itp",
"USER": "it",
"PASSWORD": "password",
"HOST": "192.168.3.111",
"PORT": "3306"
},
"jwt_key": "rRregrgERg54g564heRGRfdsger",
"voice_api_url": "https://api.crm.itpscorp.com/itpvoice",
"voice_api_key": "abc1",
"auth_api_key": "379h4f73f3279fy927yf928oowqofabdbf",
"provisioner_api_key": "abc123123",
"quote_approval_url": "http://192.112.255.145:9998/quotes/customer-approval?token=",
"docusign_base_url": "https://demo.docusign.net/restapi",
"docusign_integrator_key": "8a256bde-405b-4032-bf24-be0245631f03",
"docusign_oauth_base_url": "account-d.docusign.com",
"docusign_redirect_uri": "http://192.112.255.145:9998/api/callbacks/docusign",
"docusign_private_key_filename": "/home/itp/docusign-examples/keys/docusign_private_key.txt",
"docusign_user_id": "7f26f6bb-8a39-444f-ae99-54922fec68f6",
"docusign_user_name": "docusign#itpfiber.com"
},
"mode": "dev"
}
The empty lines after "db" starts with the unicode codepoint 0x200B ('ZERO WIDTH SPACE'). That is what trips up the JSON decoder.
I copied the text into gvim and made a screenshot. See below.
Remove those characters (or the whole line) and it works...
(Looking at the JSON file with a hex editor would also show the problem clearly.)
If you look closely at the error message, you can that this correctly identifies the problem:
ValueError: Expecting property name: line 4 column 1 (char 43)
The moral of this story: look out for whitespace codepoints.

Categories