SQLSTATE[23502]: Not null violation: 7 ERROR: null value in column - php

I am building an application with postgres as my preferred database. I have a users table and in the process of saving into it, I got the error below:
SQLSTATE[23502]: Not null violation: 7 ERROR: null value in column \"verification_code\" violates not-null constraint\nDETAIL: Failing row contains (825, 2348035442578, S-LF79a_O3ELcNc0TQmLOjy5No0KScjT, $2y$13$tPS82tr5pH2CHfNiZ9OSKOmE/z8iXOpc0PQ0uGWmeQYXtMX9AguBq, null, oyedele.phemy#gmail.com, null, 0, 11, 2020-04-25 13:54:32, 2020-04-25 13:54:32, null, null, null, null).\nThe SQL being executed was: INSERT INTO \"users\" (\"username\", \"auth_key\", \"password_hash\", \"email\", \"isActivated\", \"status\", \"created_at\", \"updated_at\") VALUES ('2348035442578', 'S-LF79a_O3ELcNc0TQmLOjy5No0KScjT', '$2y$13$tPS82tr5pH2CHfNiZ9OSKOmE/z8iXOpc0PQ0uGWmeQYXtMX9AguBq', 'oyedele.phemy#gmail.com', 0, 11, NOW(), NOW()) RETURNING \"id\"
Here is the table structure:
Table "public.users"
Column | Type | Collation | Nullable | Default
----------------------+--------------------------------+-----------+----------+-----------------------------------
id | integer | | not null | nextval('users_id_seq'::regclass)
username | character varying(255) | | not null |
auth_key | character varying(32) | | not null |
password_hash | character varying(255) | | not null |
password_reset_token | character varying(255) | | |
email | character varying(255) | | not null |
verification_code | character varying(255) | | not null |
isActivated | smallint | | not null | 0
status | smallint | | not null | 10
created_at | timestamp(0) without time zone | | not null |
updated_at | timestamp(0) without time zone | | not null |
profile_picture | character varying(55) | | |
filename | character varying(70) | | |
pin_no | integer | | |
last_login_date | date | | |
Where do you think I should check for correction or what exactly am I not doing right?

Related

MYSQL Trigger() vs. Delete permanently

I have two tables as below:
Table Job_Announcement used to store information about Job, defined as below:
+-----------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+----------------+
| job_id | int(11) | NO | PRI | NULL | auto_increment |
| job_title | varchar(255) | NO | | NULL | |
| category | varchar(255) | NO | | NULL | |
| term | varchar(255) | NO | | NULL | |
| num_experiences | int(11) | NO | | NULL | |
| num_hiring | int(11) | NO | | NULL | |
| Salary | varchar(255) | NO | | NULL | |
| qualification | varchar(255) | NO | | NULL | |
| location | varchar(255) | NO | | NULL | |
| gender | varchar(255) | NO | | NULL | |
| job_content | text | NO | | NULL | |
| job_requirement | varchar(255) | NO | | NULL | |
| publish_date | date | NO | | NULL | |
| close_date | date | NO | | NULL | |
| contact_info | varchar(255) | NO | | NULL | |
| userid | varchar(255) | NO | | NULL | |
| publish | tinyint(1) | NO | | NULL | |
+-----------------+--------------+------+-----+---------+----------------+
And Table job_announcement_deleted, used to store deleted record from table job_announcement
+--------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+----------------+
| job_id | int(11) | NO | PRI | NULL | auto_increment |
| job_title | varchar(255) | NO | | NULL | |
| category | varchar(255) | NO | | NULL | |
| job_content | text | NO | | NULL | |
| publish_date | date | NO | | NULL | |
| close_date | date | NO | | NULL | |
| userid | varchar(255) | NO | | NULL | |
| publish | tinyint(1) | NO | | NULL | |
+--------------+--------------+------+-----+---------+----------------+
In order to move deleted record from job_announcement table to job_announcement_deleted table, I defined trigger definition in phpmyadmin like this:
Trigger Name: before_delete_job
Table: job_announcement
Time: BEFORE
Event: Delete
Definition:
BEGIN
INSERT INTO job_announcement_deleted VALUES(old.job_id,old.job_title,old.category,old.job_content,old.publish_date,old.close_date,old.userid,old.publish);
END
Definer: root#localhost
The trigger event before_delete_job is working fine that the deleted record moved to table job_announcement_deleted.
My problem is if I want to restore deleted record back to table job_announcement, I define similar trigger definition event like above, ex. job_announcement_restore for tale job_announcement_deleted however, how can I do if I want to delete record permanently from job_announcement_deleted? because I want user have options either to restore it or to deleted permanantly.
Thanks.
For the restore just do the reverse process you did for delete (sending data from job_announcement_deleted to job_announcement)
For the perma delete you just have to get the job_id field, since it's auto-incremeneted making it unique for every job.
After you acquire job_id from the user (e.g if he clicks delete from this row you extract job_id from that row), just use DELETE FROM like this :
DELETE FROM job_announcement_deleted WHERE job_id = #job_id
(where #job_id will be the one you acquired earlier in the process)
Also, you shouldn't be sending job_id to the job_announcement_deleted table, since the field is auto-increment, meaning it'll add a new value (incremented by 1 by default) for that field, so there are no duplicates for that field. You just destroy the meaning of auto-increment by doing that, specially since primary key must be a unique value. You could have trouble later on with the database if you get duplicate values for that field, so I suggest you just leave that field unfilled so auto-increment can do it's job.

MariaDB JOIN fails with 1054 Column not found, but FROM...WHERE from two tables works?

I am using PDO and ran into a strange issue with JOIN vs. the alternate syntax.
PHP version: PHP 7.1.7
The PDO module is for PHP7
MariaDB: 5.5.56-MariaDB-1ubuntu0.14.04.1
When I run this:
SELECT *
FROM ShowsSetlists
JOIN Songs ON Songs.SongID = ShowsSetlists.SongID
WHERE ShowsSetlists.SongID = :id
With array('id' => $songID); as the parameter, where $songID is int(28) for example. But I get this error:
PDOException: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'ShowsSetlists.ShowID' in 'on clause'
However, if I run the same exact query (case and all identical) on both HeidiSQL on Windows-side (the DB is in Vagrant ubuntu/trusty32) and within the MySQL console on the Vagrant box, the above query works just fine - no errors and the result is normal.
The same PDOException happens with an alias:
SELECT *
FROM ShowsSetlists
JOIN Songs AS s ON s.SongID = ShowsSetlists.SongID
WHERE ShowsSetlists.SongID = :id
Now, if I build the query with the alternate syntax:
SELECT *
FROM ShowsSetlists sl, Songs s
WHERE
sl.SongID = s.SongID
AND sl.ShowID = :id
Then it works and no PDOException is thrown.
Correct me if I'm wrong, but aren't those two queries basically the same? Why the first one causes a PDOException?
I checked these questions, but they're not really related:
Column not found: 1054 Unknown column using join
join two tables, error 1054 unknown column in 'on clause'
The DB tables:
MariaDB [vortech]> DESCRIBE ShowsSetlists;
+--------------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+---------+------+-----+---------+----------------+
| SetlistID | int(11) | NO | PRI | NULL | auto_increment |
| ShowID | int(11) | YES | MUL | NULL | |
| SongID | int(11) | YES | MUL | NULL | |
| SetlistOrder | int(11) | YES | | NULL | |
+--------------+---------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
MariaDB [vortech]> DESCRIBE Songs;
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| SongID | int(11) | NO | PRI | NULL | auto_increment |
| Title | varchar(255) | YES | | NULL | |
| Duration | int(11) | YES | | NULL | |
+----------+--------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
MariaDB [vortech]> DESCRIBE Shows;
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| ShowID | int(11) | NO | PRI | NULL | auto_increment |
| ShowDate | datetime | YES | | NULL | |
| CountryCode | varchar(2) | YES | | NULL | |
| Country | varchar(100) | YES | | NULL | |
| City | varchar(100) | YES | | NULL | |
| Venue | varchar(200) | YES | | NULL | |
+-------------+--------------+------+-----+---------+----------------+
6 rows in set (0.00 sec)

Eloquent IF clause - always returns true

I am using Entrust's default table structure:
permissions table:
+--------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | UNI | NULL | |
| display_name | varchar(255) | YES | | NULL | |
| description | varchar(255) | YES | | NULL | |
| created_at | timestamp | NO | | NULL | |
| updated_at | timestamp | NO | | NULL | |
+--------------+------------------+------+-----+---------+----------------+
permission_role table:
+---------------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+------------------+------+-----+---------+-------+
| permission_id | int(10) unsigned | NO | PRI | NULL | |
| role_id | int(10) unsigned | NO | PRI | NULL | |
+---------------+------------------+------+-----+---------+-------+
roles table:
+--------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+------------------+------+-----+---------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | UNI | NULL | |
| display_name | varchar(255) | YES | | NULL | |
| description | varchar(255) | YES | | NULL | |
| created_at | timestamp | NO | | NULL | |
| updated_at | timestamp | NO | | NULL | |
+--------------+------------------+------+-----+---------+----------------+
Now, given a role_id I'd like to get select the following from this database:
permissions.id
permissions.display_name
whether the permission_role table contains an entry with the permission_id and the given role_id
The last one turned out to be a bit tricky in Eloquent.
This SQL query accomplishes exactly what I need (ID is obviously replaced by a valid role ID):
SELECT p.id, p.display_name, IF(pr.role_id = ID, 1, 0) AS has_role
FROM permissions p
LEFT OUTER JOIN permission_role pr ON p.id = pr.permission_id;
Example output:
+----+--------------+----------+
| id | display_name | has_role |
+----+--------------+----------+
| 1 | Edit users | 1 |
| 2 | View users | 0 |
| 3 | Delete users | 0 |
+----+--------------+----------+
Can anyone help me out here, on how to do this using Eloquent?
I've tried this, but it always returns 1 (true) in the third column, unlike the SQL query (as seen above).
$result = DB::table('permissions')
->leftJoin('permission_role', 'permission_role.permission_id', '=', 'permission_role.role_id')
->select(DB::raw('permissions.id, permissions.display_name, IF(permission_role.role_id = ID, 1, 0) AS has_role'))
->get();
Ideally, I'd like to do this without using DB::raw, although it is completely fine if that is what it takes.
Thanks in advance for any help!
Structurally, the Query Builder query you've shown looks fine.
What does not look fine is the left join. Shouldn't this:
->leftJoin('permission_role', 'permission_role.permission_id', '=', 'permission_role.role_id')
be this:
->leftJoin('permission_role', 'permission_role.permission_id', '=', 'permissions.id')
?

Sub-query doesn't work

Here's my schema:
Table "Questoes";
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| id_quest | int(11) | NO | | NULL | |
| questao | varchar(255) | NO | | NULL | |
| nivel | int(11) | NO | | NULL | |
| tipo | varchar(255) | NO | | NULL | |
+----------+--------------+------+-----+---------+----------------+
Table "Respostas";
+----------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| id_quest | int(11) | NO | | NULL | |
| resposta | varchar(255) | NO | | NULL | |
| r_valido | enum('0','1') | NO | | NULL | |
+----------+---------------+------+-----+---------+----------------+
My query is:
SELECT q.questao, r.resposta
FROM questoes q, respostas r
WHERE q.id_quest IN (19,20,21)
AND q.id_quest=r.id_quest
AND r.r_valido = ( SELECT resposta FROM respostas WHERE r_valido= 1 )
What I need is the field questao from table Questoes and the field resposta from table respostas where field r_valido = 1.
The field resposta have 4 results, and only one is valid, in other words, where the field r_valido = 1.
Your query should look like this:
SELECT q.questao, r.resposta FROM questoes AS q JOIN respostas AS r ON r.id_quest = q.id_quest WHERE q.id_quest IN (19,20,21) AND r.r_valido = "1"
Also I found out what is causing that weird error when you use 1 instead of "1" in the query:
We strongly recommend that you do not use numbers as enumeration
values, because it does not save on storage over the appropriate
TINYINT or SMALLINT type, and it is easy to mix up the strings and the
underlying number values (which might not be the same) if you quote
the ENUM values incorrectly
I didn't understood you completely but i think this is what you looking for:
SELECT q.questao, r.resposta
FROM questoes as q
INNER JOIN respostas as r
ON q.id_quest=r.id_quest
WHERE
q.id_quest IN (19,20,21) AND
r.r_valido = '1'

UPDATE JOIN, wrong data insertion

So i have this two tables, the operators_payments AS op is populated with data, but the op.date_paid will be NULL, till payment date arrives, when this happens, the payment_process AS pp table is used to initialize the payment (pp.date_started is set to NOW()), then for payment completion the op.date_paid is set to pp.date_started. The shown query, is used to do this, all is good, but when all records are updated, one of the records and only one gets the op.date_paid with different time, specifically the second part e.g.(time set to all but one: 2012-07-05 17:28:14, time set to one: 2012-07-05 17:28:02).
Im using Mysql 5.5, the columns have the same type (TIMESTAMP).
I need this because i need the date to be exact as the one in pp.date_started.
My question is, Why does this happens, and what can i do to have this as espected?
UPDATE operators_payments AS op
JOIN payment_process AS pp
ON op.operator_id = pp.operator_id
AND pp.type = 0
AND pp.status = 1
SET op.date_paid = pp.date_started, pp.status = 2, pp.message=CONCAT(SUBSTRING_INDEX(message, '|', 1), '| was completed successfully!')
WHERE op.operator_id = {$this->operator_id}
AND op.date_paid IS NULL
AND op.date_end <= pp.date_accounted
+---------------+-----------------------+------+-----+-------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+-----------------------+------+-----+-------------------+----------------+
| payment | int(10) unsigned | NO | PRI | NULL | auto_increment |
| operator_id | int(10) unsigned | NO | MUL | 0 | |
| date_paid | timestamp | YES | MUL | NULL | |
| date_start | timestamp | YES | | NULL | |
| date_end | timestamp | YES | MUL | NULL | |
| amount | decimal(6,4) unsigned | NO | | 0.0000 | |
+---------------+-----------------------+------+-----+-------------------+----------------+
+----------------+--------------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+--------------+------+-----+-------------------+-----------------------------+
| operator_id | int(11) | NO | PRI | NULL | |
| type | tinyint(4) | NO | PRI | NULL | |
| date_started | timestamp | YES | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| date_accounted | timestamp | YES | | NULL | |
| amount | decimal(6,4) | YES | | NULL | |
| status | tinyint(4) | YES | MUL | 0 | |
| message | varchar(255) | YES | | NULL | |
+----------------+--------------+------+-----+-------------------+-----------------------------+
I have a suspicious eye toward that on update CURRENT TIMESTAMP clause on the date_started on payment_process... I'm not actually sure what it could be doing in this query, but you are updating that table in this query, and using that value. I also don't like the semantic discord of a column called date_started which has it's value changed on every update... but I don't know how it's used. I would evaluate if that clause is necessary on that column, and see if you get this strange behavior without it,

Categories