PG::NumericValueOutOfRange: ERROR: value "34531513513513" is out of range for type integer - ruby-on-rails

I needed to test the creation of a record in the database. I went to the page with the form, randomly entered the value "34531513513513" in the title field. Selected values from select for belongs_to. And in the end I clicked on "Add".
As a result, the record was added to the database, and the controller action made me a redirect to index where I received this error:
ActiveRecord::RangeError - PG::NumericValueOutOfRange: ERROR: value "34531513513513" is out of range for type integer
The error is caused by this code:
scope :order_by_title, (lambda do
order(Arel.sql("left(lower(posts.title), 1), substring(posts.title, '\\d+')::int NULLS FIRST, posts.title"))
end)
Specifically:
substring(posts.title, '\\d+')::int
Tell me, please, how can I fix this code? But the sorting logic needs to be preserved.
P. S. I understand that for me it is most likely a very rare bug in production (because with 99% probability I will not use such large numbers in titles). But I still wonder how to solve this problem.

Related

Resolve DGET function "More than one match found" error

I am trying to match a list of range with certain criteria in a google spreadsheet. I am using DGET function for the same. Everything is working fine but the problem comes when there are many entries that contain the whole string and I receive "More than one match found in DGET evaluation.".
For the better understanding look below:
Sheet "Form Responses 1":
B
-------
Ronald
Ronaldo
Ronaldinho
Rebarto
Matching sheet entries:
A
------
Ronald
Rebarto
Juhino
My Formula is:
=DGET('Form Responses 1'!B:H,"Date",{"Email Address","Logging In or Logging out ?","Date";A2,$B$1,$H$1})
Now the problem is Ronald is matching with "Ronald","Ronaldo" and "Ronaldinho" and I am receiving the error which says "multiple entries found".
How do we solve this?
I solved the problem by Concatenating a constant variable before and after the name. For example Ronaldo becomes mRonamdom and Ronald becomes mRonaldm. This makes the Names Unique and solves the problem.
If you don't want to modify the data but to fix the formula so it doesn't get confused with similar entries in your database parameter you can add a character to the criteria field of the dget function as shown below (I'm using an '=' sign concatenated to the value I want to match with in the database parameter)
=dget(database!$A$1:$B$11,$M$1,{"columnName";"="&F2})
where
A1:B11 is my database
M1 is the matching column name
and "="&F2 is the field with the caracter I chose that I want to match with to retrieve values from the matching database column, now even if the there are more than one matches found (becuase matching substrings"), the addition of the caracter contatenated with the matching value, should take care of the in-accurate error.

Kdb+/q: How to bulk insert into a KDB+ table with an index?

I am trying to bulk insert multiple records simultaneously into a KDB+ database:
> trades:([]time:`datetime$();side:`symbol$();qty:`float$();price:`float$();exch:`symbol$();sym:`symbol$())
> t: .z.z / intentionally the same time
> `trades insert (t t;`buy `sell;10 10;10 10;`exch `exch;`sym `sym)
However It raises an error at the sym column
'sym
[0] `depths insert (t t;`buy `sell;10 10;10 10; `exch `exch;`sym `sym)
^
Have no Idea what I could be doing wrong here, but it seems to be value invariant i.e. it always raises an error on the last column irrespective of the value provided.
Could someone please advise me how I should go about inserting bulk records into kdb+ with an time index as depicted above.
Thanks
In your original insert statement, you had spaces between
`sym `sym
,
`exch `exch
and `buy `sell. The spaces between the symbols makes it an apply or index instead of a list which you desire.
Additionally, because you have specified your qty and price as
float
, you would have to specify the numbers as float when you are inserting to the
trades
table.
The following line should accomplish what you are intending to do:
`trades insert (2#t;`buy`sell;10 10f;10 10f;`exch`exch;`sym`sym)
Lastly, I would recommend changing the schema for the qtycolumn to int/long, as quantity generally does not require decimal points.
Hope this helps!
Daniel is on the money. To expand on his answer, q will collate space-separated lists into a single object for numeric values, and even then the type specification must be only present for the last item. Further details on list creation can be found here.
q)a:10f 10f
'10f
q)a:10 10f
Secondly, it's common for those learning kdb to often encounter type errors when appending to tables. The problem in this case is that kdb is not promoting a list of homogeneous atoms to a wider type (which is expected behaviour). The following is a useful little lambda for letting you know where you are going wrong when performing insert or upsert operations:
q)trades:([]time:`datetime$();side:`symbol$();qty:`float$();price:`float$();exch:`symbol$();sym:`symbol$())
q)rows:(t,t;`buy`sell;10 10;10 10;`exch`exch;`sym`sym)
q)insertTest:{[tab;rows] m:0!meta tab; wh: where not m[`t] ~' rt:.Q.ty each rows; #[flip;;enlist] `item`currType`expectedType!(m[`c] wh;rt wh; m[`t] wh)}
item currType expectedType
---------------------------
qty j f
price j f

Invalid name that I didn't actually write

I'm attempting to use a MS Access query update to take the value of one field, the value of another in a different table, multiply them together, and then put the result in a third table. When just doing it as a SELECT statement, the statement works perfectly fine - the table output is exactly what is expected. However, when specifying an update query, it tells me the name is invalid, and the invalid name is not the one I wrote in.
The error:
'PO Lines.Quantityity]*[Item Master].[Purchased Price]' is not a valid name. Make sure that it does not include invalid characters or punctuation and that it is not too long.
The statement:
EXPR: [PO Lines].[Quantity]*[Item Master].[Purchased Price]
(with Update To as [PO Lines].[Price] and Table as PO Lines). If any more information is needed, please inform me; however, I wouldn't even know where to begin. All fields exist - as I said, the statement worked perfectly well as a Select.

How to find document with SOLR query and exact string match

Considering a simple table:
CREATE TABLE transactions (
enterprise_id uuid,
transaction_id text,
state text,
PRIMARY KEY ((enterprise_id, transaction_id))
and Solr core with default, auto-generated parameters.
How do I construct a Solr query that will find me record(s) in this table that have state value exact match to an input, considering the state can be arbitrary string?
I tried this with state value of a+b. This works fine with q=state:"a+b", but that creates a "phrase query":
"rawquerystring": "state:\"a+b\"",
"querystring": "state:\"a+b\"",
"parsedquery": "PhraseQuery(state:\"a b\")",
"parsedquery_toString": "state:\"a b\"",
So, the same record is found if I use query like q=state:"a(b", which results into the same phrased query and finds the record with state of a+b. That is unacceptable to me, because I need an exact match.
I went through https://cwiki.apache.org/confluence/display/solr/Other+Parsers, and tried using q={!term f=state}a+b or q={!raw f=state}a+b, but neither even finds my sample transaction record.
Probably you got state generated as a TextField where standard tokenization is applied StandardTokenizer and then a split is made on + and the plus sign itself is discarded. You could use a different tokenizer (whitespace?) or just make state an StrField for an exact match.
This works for me with state as an StrField:
select * from transactions where solr_query='state:a+b';

Record not always found

Consider the following
# setup an array of the question ids so far
questions_array = []
questions_array.push(session[:questions_array])
# take a random question whom id is not included in the session[:questions_array]
#question = Question.offset(rand(Question.count)).where('id NOT IN (?)',questions_array).take
# push id to array and later on assign the new array to the session
questions_array.push(#question.id)
session[:questions_array] = questions_array
I have two questions database. One of the two gets returned the other one gives me the error
NoMethodError (undefined method 'id' for nil:NilClass):
this line gives the error questions_array.push(#question.id)
this does not happen everytime! and that is what's strange!
This is how, you can solve it though:
Question
.where.not(id: questions_array)
.order('random()')
.first
But if the questions table get say more than 10,000 records for example, it will be slow. Then you could write recursive procedure, which will pick a random record and check some condition. if condition matches, returns the record, or recursion will go on with some base condition to break in worst case.
My bad seems that the problem lies in the fact that i first get a random id and then check if that id is not in the array which is not what i wanted. So if the random id was included in the array it would give an error as there is none to take.
this is the new line of code.
#question = Question.where('id NOT IN (?)',questions_array).first

Resources