select column in hibernate does not work but * does - sql

I have this:
Query q = em.createNativeQuery("select * from TRANSAKTION",Transaktion.class);
List<Transaktion> trans = q.getResultList();
While this works, using a column name like id doesn't although it exists in database and generated entity class.
Other tables work fine it's just that one and with all columns. When I use * and .getId() it returns the correct id.
What could possibly be the problem?
The error message is:
SQL Exception: invalid column name

Related

Why am I getting the error: "Ambiguous column" in my query?

In this query I inserting records into a new empty table I created. These records are derived from another table where I am left joining that table to itself, in order to output records that are not included in the recent table that is appended on top of an older table. So basically it outputs records that were deleted.
CREATE DEFINER=`definer` PROCEDURE `stored_procedure_name`()
MODIFIES SQL DATA
SQL SECURITY INVOKER
BEGIN
START TRANSACTION;
INSERT INTO exceptions_table (
`insert_date`,
`updated`,
`account_number`,
`id_number`)
SELECT
`insert_date`,
`updated`,
`account_number`,
`id_number`
FROM original_table ot1
LEFT JOIN original_table ot2
ON ot1.`account_number` = vdcaas2.`account_number`
AND ot2.`insert_date` = '2022-12-20'
WHERE ot1.`insert_date` = '2022-12-10'
AND ot2.`account_number` IS NULL;
COMMIT;
END
I get an error stating: "SQL Error: Column "insert_date" in field list is ambiguous.
I'm not sure why because I have specified which table I am grabbing "insert_date" from when INSERTING and when SELECTING and JOINING..
Every row in your query has two columns called insert_date: one from the table you've aliased as "ot1", and one from the table (as it happens, the same table) you've aliased as "ot2".
The database system doesn't know which one you want, so you have to tell it by writing either "ot1.insert_date" or "ot2.insert_date", just as you do elsewhere in the query:
... ot2.`insert_date` = '2022-12-20'
...
... ot1.`insert_date` = '2022-12-10'
The same is true of the other columns you've listed to select.
You need to change this
SELECT
`insert_date`,
`updated`,
`account_number`,
`id_number`
to this
SELECT
ot1.`insert_date`,
ot1.`updated`,
ot1.`account_number`,
ot1.`id_number`
or this
SELECT
ot2.`insert_date`,
ot2.`updated`,
ot2.`account_number`,
ot2.`id_number`
or some combination
Issue
SQL Error: Column "insert_date" in field list is ambiguous error means that the query is trying to reference the "insert_date" column from both tables, ot1 and ot2.
Try the following:
SELECT
ot1.`insert_date`,
ot1.`updated`,
ot1.`account_number`,
ot1.`id_number`
Also, you have a typo in your query:
ON ot1.`account_number` = vdcaas2.`account_number` -> ON ot1.`account_number` = ot2.`account_number`

ERR:Unknown column in where clause when query with struct

db.Table("test").Where(&Query{Name:"test"}).Find(&infos)
When I execute this statement, the SQL statement looks like this:
SELECT * FROM `test` WHERE (`queries`.`name` = 'test')
And the error:
Error 1054: Unknown column 'queries.name' in 'where clause'
It uses the name of my struct instead of the table name
Are you running the latest version of Gorm v2? I cannot reproduce what you see: the code you wrote produces
SELECT * FROM `test` WHERE `test`.`name` = "test"
In any case, if you don't use the Query struct, but instead write out the condition it should work:
db.Table("test").Where("name = ?", "test").Find(&infos)
Or you can use a map:
db.Table("test").Where(map[string]interface{}{"Name": "test"}).Find(&infos)

SQL Parameter to Include All on ID Column

I'm just taking a look at the following query
select * from tablename
where id like '%%';
So that it can handle parameters to include all of the data or filtered data like bellow
select * from tablename
where id like '%1%';
Which is fine for most parameters I use but this seems wrong for an ID because it will return all data that has IDs containing 1 which I don't want
To get around this I can only append the where clause if the ID is given but that seems like a pain in the butt
Is it possible to use a different type of where clause so that a wildcard can be used in a where equals clause instead of a where like clause, example
select * from tablename
where id = '*';
So that the same query can be used to return all or filtered data? Pass parameter '*' for all or parameter '1' for ID 1 specifically
(I'm not sure if it matters for this case but I'm using PostgreSQL 9.6.12 in this example)
This would often be expressed as:
where (id = :id or :id is null)
null is the "magic" value that represents all rows.

How to query a Postgres `RECORD` datatype

I have a query that will return a row as a RECORD data type from a subquery - see below for example:
select *
from (
select row(st.*) table_rows
from some_table st
) x
where table_rows[0] = 339787
I am trying to further qualify it in the WHERE clause and I need to do so by extracting one of the nodes in the returned RECORD data type.
When I do the above, I get an error saying:
ERROR: cannot subscript type record because it is not an array
Does anybody know of a way of implementing this?
Use (row).column_name. You can just refer to the table itself to create the record:
select *
from (
select r
from some_table r
) x
where (r).column_name = 339787
There is a small chance that later a column is created with the same name as the alias you chose and the above query will fail as select r will return the later created column in instead of the record. The first solution is to use the row constructor as you did in your question:
select row(r.*) as r
The second solution is to use the schema qualified name of the table:
select my_squema.some_table as r
Alternately You can try this
select *
from (
select *
from tbl
) x
where x.col_name = 339787

Possible error not shown at SQLite manager

I have a table 'student' in which I have five attributes {id,first,last,age,marks}.
The following are the two tuples inserted.
1,Suresh,Kumar,35,95
2,ramesh,Kapoor,21,90
When I execute the query with mistake
select * from stdent where first='Suresh'
I get the alert message - no such table stdent.Very fair!
But when I execute the following query
select * from student where first='sachin'
I get no alerts or error message even though sachin is not present in any of the tuple.
What is the reason?
It's because the query below is correct. No issues with the query itself. However, no tuple exists with first=sachin. Therefore, nothing is returned.
select * from student where first='sachin'
If you try
select * from student where first='Suresh'
then the query will successfully return 1 row, as a tuple with first=Suresh exists.