Getting Numeric value out of range, but I've created it BigInt - php

I have a table, and one of the columns is request_id, which is defined in a migration like this:
$table->bigInteger('request_id')->index()->unsigned()->nullable();
And when I look on phpmyadmin at the table structure, it says
bigint(20) UNSIGNED
I'm making a request to my api, and sending through this value: 1562247865319
$requestLog = new RequestLog();
$requestLog->request_id = 1562247865319;
$requestLog->save();
This code is erroring and sending back Numeric value out of range: 1264 Out of range
What's really strange is that when I'm in phpmyadmin, I can manually set the value of that column to much bigger values than 1562247865319, but when I do it through my laravel model it doesn't like it.
edit
error message is
SQLSTATE[22003]: Numeric value out of range: 1264 Out of range value for column 'request_id' at row 1 (SQL: insert into `request_logs` (`ip`, `submitted_user_name`, `url`, `full_url`, `method`, `request_id`, `request_data`, `api_user_id`, `updated_at`, `created_at`) values (..., ..., ..., http://..../api/..., POST, 1562247865319, {"request_id":"1562248616206"}, 2, 2019-07-04 14:57:00, 2019-07-04 14:57:00))

I think this issue is the same than this.
Try to change any UNSIGNED ints to SIGNED.

Related

How to insert DATE into AZURE SQL DATABASE with PHP and AZURE SQL CONSOLE

PROBLEM
Recently, I have created the table with PHP and it worked.
$woQuery = "CREATE TABLE WOQUERY (
nameNO VARCHAR(30) NOT NULL,
commissionDate DATE,
serviceType TEXT NOT NULL,
serviceNo TEXT NOT NULL,
userNameC CHAR(30),
dateAssigned DATE);
However, when I tried to insert date
/* Proceeds with the upload */
$sql = "INSERT INTO WOQUERY VALUES(?,?,?,?);";
/* array(nameWO, commissionDate , serviceType, serviceNo) */
$params = array($extractor[0], $extractor[1], $extractor[2], $extractor[3]);
if (!sqlsrv_query($databaseController, $sql, $params)) {
die(print_r(sqlsrv_errors(), true));
return false;
} else {
print("Successfully uploaded");
return true;
}
My program kept stopping at die. Saying that Incorrect syntax. Tho I went around stackoverflow to look for similar problem none of them solved my issue.
This led me to go into Microsoft AZURE SQL's console to test out.
INSERT INTO WOQUERY ('xadsada', '28/01/2019', 'helpService', '12345', 'johnny' , '03/02/2019');
Despite trying to use FORMAT() or CAST() or just leaving it as '28/01/2019'.
I will get errors like "Conversion failed when converting date/time from string" or "string or binary data would be truncated".
HELP
I was wondering if there is any way for me to insert the date into AZURE SQL (not msssql/sqlite/mysql)?
Thanks.
Your values are not matching with the types of columns, however query looks correct. You need to send a value of data type DATE for the date columns or change it to String type or send CURRENT_TIMESTAMP
Because you wrote wrong insert string. You did not declare columns name before declare values.
example insert string:
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
View document in here
The only way to insert date is if you follow 'mm-dd-yyyy' format and not 'dd-mm-yyyy'. Hence the only way I can think of now is to change my column datatype to string and store the date.
If I ever need the date I will get the date from SQL and then convert it into date format.

INSERT ON DUPLICATE KEY UPDATE multiple rows, checking duplicate for each row

The expected behavior of the following code is that for each row, it checks if there is a duplicate, if so, it updates instead of inserting. It does this for EACH row separately, meaning that finding a duplicate for a certain row will not result into UPDATE for all other rows that follow. The UNIQUE key is member_id.
INSERT INTO table
(member_id, member_name, stat1, stat2) VALUES
('1', 'user1', '1411', '1410'),
('15', 'user2', '177', '179'),
('83', 'user3', '517', '832'),
('184', 'user4', '805', '1165'),
('304', 'user5', '708', '705')
ON DUPLICATE KEY UPDATE
stat1 = VALUES(stat1),
stat2 = VALUES(stat2)
What this seems to do however, is UPDATE from the first moment it finds a duplicate key for member_id.
E.g. user2 already exists, so instead of INSERT, do UPDATE. But then it does UPDATE for user3, user4, user5 even though they might not exist. This causes the following error:
Unknown column 'user3' in 'field list'
How do I ensure that it checks for duplicate for EACH row and if there is no duplicate, insert?
EDIT:
A more accurate snippet of my query:
INSERT INTO reputation_p_month (member_id, member_name, january_2018_rep, acc_january_2018_rep) VALUES
('1', 'GreatJackal', '1411', '1410'),
('15', 'wetletus', '177', '177'),
('83', 'strongandbald', '517', '517'),
('14375', 'Newer', '0', '0'),
('14379', 'RobsenMeister', '0', '0'),
(14405, Griffin, 0, 0)
ON DUPLICATE KEY UPDATE
january_2018_rep = VALUES(january_2018_rep),
acc_january_2018_rep = VALUES(acc_january_2018_rep)
With the following error in console:
Uncaught SyntaxError: missing ) after argument list
'Debug Objects: Error updating records for REP: Unknown column 'Griffin' in 'field list''
Your Query is perfectly fine and should exactly produce the result you are expecting.
The Error Message
Unknown column 'user3' in 'field list'
actually means, that your query is considering the Value: user3 as column at some point.
From the query posted, we can't determine the problem, i'll guess you simplified / obfuscated the query in a certain way, but the postet example was correct by accident.
Check, if you are inserting your user value for user3 enclosed by backticks, like
`user3`
instead of
'user3'
(Back-ticking is used for COLUMN names, not values, which then would produce exactly the error message you are facing)

CakePHP: Unique violation: 7 ERROR: duplicate key value violates unique constraint

I'm running into the following error after trying to delete a bunch of records and then insert new ones:
Error: SQLSTATE[23505]: Unique violation: 7 ERROR: duplicate key value violates unique constraint "routes_pkey" DETAIL: Key (id)=(1328) already exists.
SQL Query:
INSERT INTO routes (agency_id, route_identifier, short_name, long_name, description, route_color, text_color) VALUES (:c0, :c1, :c2, :c3, :c4, :c5, :c6) RETURNING *
Here's a simplified version of the code:
$routes = TableRegistry::get('Routes');
$routes->deleteAll(['agency_id' => $agency->id]);
# Data to be updated
$patchData = [
'stops' => $stopData,
'static_data_modified' => Time::now()
];
$patchOptions = [
'associated' => ['Stops.Routes']
];
# If: The stops have already been added to the DB
# Then: Remove them from the patch data
if (isset($stopData['_ids'])) {
unset($patchData['stops']);
# Change settings for this implementation type
$patchOptions = [];
$stopCount = count($stopData['_ids']);
}
$agency = $this->Agencies->patchEntity($agency, $patchData, $patchOptions);
$this->Agencies->save($agency);
It seems like for some reason Postgres thinks I'm inserting a record with a duplicate primary key. But I can't see how that would be possible from my code.
Here's what I see at the end of the SQL Log:
DELETE FROM
routes
WHERE
agency_id = 51
BEGIN
SELECT
1 AS "existing"
FROM
agencies Agencies
WHERE
Agencies.id = 51
LIMIT
1
INSERT INTO routes (
agency_id, route_identifier, short_name,
long_name, description, route_color,
text_color
)
VALUES
(
51, '100001', '1', '', 'Kinnear - Downtown Seattle',
'', ''
) RETURNING *
ROLLBACK
Any ideas why I'm seeing this error?
I'm on CakePHP v3.1 with Postgresql 9.4
I tried to add this but it didn't change anything:
$connection = ConnectionManager::get('default');
$results = $connection->execute('SET CONSTRAINT = routes_pkey DEFERRED');
Here are similar questions I've read without finding a solution:
ERROR: duplicate key value violates unique constraint in postgres
cakephp duplicate key value violates unique constraint
Error: duplicate key value violates unique constraint
ERROR: duplicate key value violates unique constraint "xak1fact_dim_relationship"
postgresql: error duplicate key value violates unique constraint
https://stackoverflow.com/questions/33416321/postgresql-bdr-error-duplicate-key-value-violates-unique-constraint-bdr-node
UPDATE
Following muistooshort's comment, I deleted all records from the routes table and re-ran the code and it worked fine. It also worked fine when I ran it a second time after that. So I think this supports mu's theory that something is wrong with the existing records in the db (not my code). I think the better question now is what exactly are the circumstances in the DB that are causing this and how do I fix them?
The serial type in PostgreSQL is pretty simple: it is essentially an integer column whose default value comes from a sequence. But the sequence doesn't know what you're doing to the table so things can get confused if you specify a value for the serial without using or updating the sequence.
For example:
create table t (
id serial not null primary key
);
insert into t (id) values (1);
insert into t (id) values (DEFAULT);
will produce a uniqueness violation because 1 was explicitly used for id but the sequence had no way of knowing that it was used.
Demo: http://sqlfiddle.com/#!15/17534/1
Presumably somewhere at sometime something added a row with id = 1328 without that id coming from the sequence. Or perhaps the sequence used for your PK's default was adjusted using setval to start returning values that were already in the table.
In any case, the easiest thing to do is adjust the sequence to match the table's current content using setval:
select setval('routes_id_seq', (select max(id) from routes));
The sequence should be called routes_id_seq but if it isn't, you can use \d routes inside psql to find out what its name is.
So if we update the previous example to this:
create table t (
id serial not null primary key
);
insert into t (id) values (1);
select setval('t_id_seq', (select max(id) from t));
insert into t (id) values (DEFAULT);
then we'll get 1 and 2 in our table instead of 1 and an error.
Demo: http://sqlfiddle.com/#!15/17534/7

LIKE % XX(YY)GG % not working in MySQL

I have used Stored procedure to check whether a name exist in a table or not by using the following code snippet..
BEGIN
IF ids = 0 THEN
SELECT * FROM table_name WHERE `table_id` = alb_id AND name LIKE CONCAT('%',var_name,'%');
END IF;
END
I got the solution if the name doesn't contain any special characters or brackets, like XXXX.
If the name contains any brackets means result not came, like XX(YY)GG.
Suggest me for the best solution
Edited:
In this if a name exist already i should not insert it again, for this condition i used this procedure. If it returns mysql_num_rows > 0 means i wont insert, else i will insert the name into my table..
My sample names are,..
Turning Tables (Live Acoustic)
Hiding My Heart
Someone Like You (Live Acoustic)
Right Now (Na Na Na)
Keep You Much Longer
Someone Like You
In the list of name "Someone Like You" and "Someone Like You (Live Acoustic)" are two different names, i want to identify the name "Someone Like You (Live Acoustic)" is already exist or not..
How do i do?
CREATE TABLE `stack_test` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`text` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB CHARSET=utf8;
INSERT INTO `blur_new`.`stack_test` (`id`, `text`) VALUES ('1', 'run (rabbit) run');
INSERT INTO `blur_new`.`stack_test` (`id`, `text`) VALUES ('2', 'test');
set #a = 'n (rabbit)';
select * from stack_test where text like concat('%',#a,'%');
results in:
1 |run (rabbit) run
So, it works.
check the rest of the conditions in where clause
check how are you passing the value
you might gonna have to check you data types. I have a feeling something is not right there

phpcassa gives error on cql execution

I have recently created a column family with CREATE TABLE in cqlsh console.
I have the following column family;
CREATE TABLE user (
id timeuuid PRIMARY KEY,
balance decimal,
is_free boolean,
player_id bigint,
)
I want to insert the following type of data;
{
"player_id": 104,
"balance": "0.00",
"is_free": false,
"bonus": "3+3+3"
}
when I do insert from cqlsh console the insertion is the following;
INSERT INTO keyspace.user (player_id, balance,is_free,bonus, id)
VALUES (104,0.00,'3+3+3',67c5cb00-d814-11e2-9e13-59eb8e4538e5)
Id is generated by UUID::uuid1()->string
When I try to insert from cqlsh, it gives no error. However, in phpcassa, it gives the following error :
Expected key 'ID' to be present in WHERE clause for 'users'
I already set the client's cql version to 3.0.0 by
$pool->get()->client->set_cql_version("3.0.0");
I already tried to insert timeuuid field like that '67c5cb00-d814-11e2-9e13-59eb8e4538e5'
By the way, the var_dump of the variable $cql that is executed is the following;
string 'INSERT INTO keyspace.user (player_id,balance,is_free,bonus,id) VALUES (104,0.00,false,'3+3+3','ca256040-d819-11e2-ae08-f78171d975c3')'
What is the problem here ?
execute_cql_query() function does not seem to be working in cql_version 3.0.0
although cql 3 support is not official in phpcassa, execute_cql3_query() function works stable & well for me by using the following code;
$this->_connection_pool->get()->client->execute_cql3_query($cql, ConsistencyLevel::ONE);
where $cql is the following;
string 'INSERT INTO keyspace.user (player_id,balance,is_free,bonus,id)
VALUES (104,0.00,false,'3+3+3',12128260-d8ea-11e2-b5b3-0be38f9377b6)'

Categories