Laravel postgresql join error. "Operator does not exist" - laravel

My laravel query code
$query = \DB::table('sample_point_mission')
->rightJoin('sample_point_record', 'sample_point_mission._id', '=','sample_point_record._pid')
->leftJoin('animal_name', 'sample_point_record.animal_name_id', '=','animal_name.tid::VARCHAR)')
->leftJoin('geographic_unit', 'sample_point_record.geography_unit_id', '=','geographic_unit.tid::VARCHAR');
$query->get();
error message ==>
SQLSTATE[42883]: Undefined function: 7 ERROR: operator does not exist: character varying = integer
So I try to add the character it needs
$query = \DB::table('sample_point_mission')
->rightJoin('sample_point_record', 'sample_point_mission._id', '=','sample_point_record._pid')
->leftJoin('animal_name', 'sample_point_record.animal_name_id', '=','animal_name.tid::VARCHAR')
->leftJoin('geographic_unit', 'sample_point_record.geography_unit_id', '=','geographic_unit.tid::VARCHAR');
$query->get();
error message :
SQLSTATE[42703]: Undefined column: 7 ERROR: column animal_name.tid::VARCHAR does not exist

I don't think you can use raw Postgres SQL inside the a regular Laravel join function. But, you can (and probably have to) use raw SQL with DB::raw. Try this version:
$query = \DB::table('sample_point_mission')
->rightJoin('sample_point_record', 'sample_point_mission._id', '=','sample_point_record._pid')
->leftJoin('animal_name a', function($join) {
$join->on(DB::raw("a.tid::VARCHAR"), DB::raw("="), DB::raw("sample_point_record.animal_name_i")) })
->leftJoin('geographic_unit g', function($join) {
$join->on(DB::raw("g.tid::VARCHAR"), DB::raw("="), DB::raw("sample_point_record.geography_unit_id")) })
$query->get();

Related

[IBM][CLI Driver] CLI0125E Function sequence error on RUBY

require "ibm_db"
=> true
db_config = {:host=>"ec2-<>.compute.amazonaws.com", :database=>"SAMPLE", :user=>"user", :password=>"pass", :port=>50000}
db_conn = IBM_DB.connect("DATABASE=#{db_config[:database]};HOSTNAME=#{db_config[:host]};PORT=#{db_config[:port]};PROTOCOL=TCPIP;UID=#{db_config[:user]};PWD=#{db_config[:password]};AUTHENTICATION=SERVER;ClientWrkStnName=tester", "", "")
=> #<IBM_DB::Connection:0x00007fa563fbc8f8>
IBM_DB.autocommit(db_conn)
=> 1
IBM_DB.autocommit(db_conn,0)
=> true
IBM_DB.autocommit(db_conn)
=> 0
sql = "INSERT INTO TTE (name, price) VALUES (?,?)"
stmt = IBM_DB.prepare(db_conn, sql)
#<IBM_DB::Statement:0x00007fa564ce28c0>
value = "string"
IBM_DB.bind_param(stmt,1,value)
(pry):12: warning: Describe Param Failed: [IBM][CLI Driver] CLI0125E Function sequence error. SQLSTATE=HY010 SQLCODE=-99999
=> false
tried another way
param = ["sr", 1]
=> ["sr", 1]
IBM_DB.execute(stmt, param)
(pry):14: warning: Execute Failed due to: [IBM][CLI Driver] CLI0125E Function sequence error. SQLSTATE=HY010 SQLCODE=-99999
=> false
Getting CLI0125E Function sequence error for both ways. Not sure how to resolve it.
I'm on Mac catalina, using ibm_db (3.0.5)
.zschrc
export IBM_DB_HOME=/Applications/dsdriver
export DYLD_LIBRARY_PATH=/Applications/dsdriver/lib
export LD_LIBRARY_PATH=/Applications/dsdriver/lib
There was a mismatch in the table scheme. The field price was not present. Corrected the table schema and the query is working.

Laravel Illegal operator and value combination on where clause

There are more than 10 controllers in my project. They all have the same code, only the table names are different.
Exactly 2 of these controllers have a problem with the shared host that is 'Illegal operator and value combination'. Surprisingly other controllers works without making such a mistake.
Maybe I overlooked something.
That's where the 'Illegal operator and value combination' problem comes from.
$tarifs = DB::table('tarifs as tarif')
->join('companies as comp', 'tarif.company_id','=', 'comp.id')
->join('categories as cats', 'tarif.catid', '=', 'cats.id')
->where('tarif.company_id', '<', env('COMKEY'))
->get(['tarif.*', 'comp.name', 'comp.photo as cimage','cats.cat_uz']);
Showing this line as an error ->where('tarif.company_id', '<', env('COMKEY'))
This one works without errors.
$minutes = DB::table('minutes as minute')
->join('companies as comp', 'minute.company_id','=', 'comp.id')
->join('categories as cats', 'minute.catid', '=', 'cats.id')
->where('minute.company_id', '<', env('COMKEY'))
->get(['minute.*', 'comp.name', 'cats.cat_uz']);
The project is running normally and without errors on localhost.
Shared and local php versions are the same 7.4
When env('COMKEY') returns the null you will get this error:
InvalidArgumentException with message 'Illegal operator and value combination.'
Code:
$minutes = DB::table('tarifs as tarif')
->join('companies as comp', 'tarif.company_id','=', 'comp.id')
->join('categories as cats', 'tarif.catid', '=', 'cats.id')
->when($comKey,function($query) use ($comKey){
logger()->info('Comkey value is not null',[
'comKey'=> $comKey
]);
return $query->where('tarif.company_id', '<', env('COMKEY'));
},function($query) use ($comKey){
logger()->info('Comkey value is null');
return $query;
})
->dd();
Please use the above query to debug your query.
If the value is returned from env the log will be something like
Comkey value is not null {"comKey":"RandomKey"}
If the value is returned from env the log will be something like:
Comkey value is null

Laravel eager loading Postgres

I'm using postgres DB and I don't know how to eager loading a model with Laravel, maybe is not supported with postgres??
SQLSTATE[42883]: Undefined function: 7 ERROR: X does not exist
$company->operators
Illuminate\Database\QueryException: SQLSTATE[42883]: Undefined function: 7 ERROR: operator does not exist: character varying = integer
LINE 1: ...elect * from "operator" where "operator"."companyid" in (76)
Laravel uses Where IN

CI_session and Error Number: 1054 (Unknown column ...)

I have got problem with ci_session. When I am creating query with Active Record and I use session, it throws me Error Number: 1054.
It is caused by Session.php . I am using database session and when it executes sess_write(), then it throws error.
Can I create new instance of $this->db ?
Error is at line no. 289 in Session.php
$this->CI->db->update($this->sess_table_name, array('last_activity' => $this->userdata['last_activity'], 'user_data' => $custom_userdata));
In this place is set $this->CI->db->order_by(‘something’), from my code, before I call $this->session and this is my problem.
EDIT (13:12):
Unknown column 'contract.created' in 'order clause'
UPDATE `ci_sessions` SET `last_activity` = 1393330303, `user_data` = 'some_data' WHERE `session_id` = 'ea330d7194f902b6b38b88d509766560' ORDER BY `contract`.`created` DESC LIMIT 30
Filename: C:\Apache24\htdocs\Trokadero_v5\system\database\DB_driver.php
EDIT 2:
I am sure, it is happening by Active Record , because sequence of AR tasks is:
My_controller.php
$this->db->order_by('contract.created', 'DESC');
$this->session->set_flashdata('order_by_direction', 'DESC');
And then is executing in Session.php, with my own order_by clause
$this->CI->db->update($this->sess_table_name, array('last_activity' => $this->userdata['last_activity'], 'user_data' => $custom_userdata));
I got it.
$this->session->userdata();
$this->session->set_userdata();
etc.
MUST BE used before/after execute your own CRUD ($this->db->...).
In your controller, you have to reset the db query.
$this->db->order_by('contract.created', 'DESC');
// add line of code to get data. After getting data, reset the db object.
$this->db->_reset_write();
$this->session->set_flashdata('order_by_direction', 'DESC');

How to properly use Alias in Codeigniter

Here is my code:
$this->db->select('course_name AS Course Name,course_desc AS Course Description,display_public AS Display Status',FALSE);
$this->db->from('courses');
$this->db->where('tennant_id',$tennant_id);
$this->db->order_by('course_name','ASC');
$query = $this->db->get();
and I got an error:
A Database Error Occurred
Error Number: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Name, course_desc AS Course Description, display_public AS Display Status FROM (' at line 1
and I got an error:
A Database Error Occurred
Error Number: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Name, course_desc AS Course Description, display_public AS Display Status FROM (' at line 1
SELECT course_name AS Course Name,
course_desc AS Course Description,
display_public AS Display Status
FROM (`courses`) WHERE `tennant_id` = 'elicuarto#apploma.com'
ORDER BY `course_name` ASC
Filename: C:\wamp\www\coursebooking\system\database\DB_driver.php
Line Number: 330
Try
$this->db->select('course_name AS `Course Name`, course_desc AS `Course Description`, display_public AS `Display Status`', FALSE);
It's the space in your alias that is messing with you.
UPDATE
I'm not sure why you would want to, but I see nothing preventing you from writing
$this->db->select("course_name AS `{$variable}`", FALSE);
(showing just one field for simplicity)
UPDATE 2
Should be standard string conversion so I don't know why it doesn't work for you.. there's always split strings...
$this->db->select('course_name AS `' . $variable . '`', FALSE);

Resources