I'm Running the following query without any problem in MySQL (5.7.21) Database but this query throws error on my hosting server database which is MariaDB (10.1.47-MariaDB) and I don't have super permission to change the sql mode.
$projects = V2Project::leftJoin('v2_project_locations as v2pl', 'v2pl.project_id', 'v2_projects.id')
->leftJoin('v2_locations as v2l', 'v2l.id', 'v2pl.location_id')
->selectRaw('v2_projects.id, v2_projects.name, group_concat(v2l.name) as locations')
->withDepartmentInMind()
->onlyAuthorizedObjects();
if($by_current_step == true) {
$projects = $projects->where('current_step', 'plan');
}
if($search_val) {
$projects = $projects->whereIn('v2_projects.id', $search_val);
}
$projects = $projects->groupBy('v2_projects.id')->paginate(10);
$projects->appends(Input::except('page', '_token'));
Error:
SQLSTATE[42000]: Syntax error or access violation: 1055 'pm_form_moe.v2_projects.name' isn't in GROUP BY (SQL: select v2_projects.id, v2_projects.name from `v2_projects` group by `v2_projects`.`id`)
I also tried to add every column, but I need the functionality the same as mySQL without adding all columns.
Read about ONLY_FULL_GROUP_BY, then decide what to remove from your SELECT or what to add to the GROUP BY. Don't worry about setting the flag; your code is bad and needs to be fixed. And it was bad in the older version when it did not complain.
(If you show us the generated SQL, we can discuss it further.)
Related
I was trying to run this query in laravel:
$siswa = DB::table('siswa')
->join('pengambilankelas', 'siswa.id_siswa', '=', 'pengambilankelas.id_siswa')
->join('subcpmkpengambilan', 'pengambilankelas.id_pengambilanKelas', '=', 'subcpmkpengambilan.id_pengambilanKelas')
->join('tesformatif', 'subcpmkpengambilan.id_subcpmkpengambilan', '=', 'tesformatif.id_subcpmkpengambilan')
->select('siswa.*', 'tesformatif.*', DB::raw('max(tesformatif.nilai_tesFormatif) as max_nilai'))
->groupBy('siswa.id_siswa')
->where('pengambilankelas.id_kelas','=', $id_kelas)
->where('subcpmkpengambilan.id_subCPMK', '=', $currentSubcpmk->id_subCpmk)
->where('subcpmkpengambilan.status_subcpmkpengambilan', '=', 3)
->get();
then i got this error:
SQLSTATE[42000]: Syntax error or access violation: 1055 'dil.siswa.identitas_siswa' isn't in GROUP BY (SQL: select `siswa`.*, `tesformatif`.*, max(tesformatif.nilai_tesFormatif) as max_nilai from `siswa` inner join `pengambilankelas` on `siswa`.`id_siswa` = `pengambilankelas`.`id_siswa` inner join `subcpmkpengambilan` on `pengambilankelas`.`id_pengambilanKelas` = `subcpmkpengambilan`.`id_pengambilanKelas` inner join `tesformatif` on `subcpmkpengambilan`.`id_subcpmkpengambilan` = `tesformatif`.`id_subcpmkpengambilan` where `pengambilankelas`.`id_kelas` = 5 and `subcpmkpengambilan`.`id_subCPMK` = 6 and `subcpmkpengambilan`.`status_subcpmkpengambilan` = 3 group by `siswa`.`id_siswa`)
Query run fine outside laravel. this post tell me to disable strict mode
and it works.
is there a better way without disabling strict mode? is it even save to do so?
ps. I'm sorry the database is in indonesian.
You may have the sql_mode ONLY_FULL_GROUP_BY enabled. To disable it run in the mysql console the following command:
mysql > SET GLOBAL sql_mode=(SELECT REPLACE(##sql_mode,'ONLY_FULL_GROUP_BY',''));
References
Disable ONLY_FULL_GROUP_BY
I'm little wonder please help me out.
My query is :- Invoice::join('orders', 'orders.invoice_id', '=', 'invoices.id')->groupBy('invoices.id')->get();
Then I have got an error: Syntax error or access violation: 1055 'invoices.reference_number' isn't in GROUP BY
When I updated my query according to error then the query is:-
$invoices = Invoice::join('orders', 'orders.invoice_id', '=', 'invoices.id')->groupBy('invoices.id','invoices.reference_number','invoices.customer_address_id','invoices.country_id','invoices.paid_at','invoices.due_date','invoices.created_at','invoices.updated_at','invoices.deleted_at','orders.id','orders.currency_id','orders.user_id','orders.customer_id','orders.created_at','invoices.updated_at')->get();
Now query working but in that case, I can't use select(), It returns columns who is in the groupBy()
So I did google then found if I do database strict mode off then it will work and then I did and it's working good, But I don't want strict mode turned off.
Also, i did the study about database modes https://dev.mysql.com/doc/refman/5.5/en/faqs-sql-modes.html https://dev.mysql.com/doc/refman/5.5/en/sql-mode.html
Then I found query:- SELECT ##GLOBAL.sql_mode; SELECT ##SESSION.sql_mode;
But the result is blank
If I do in the database - select * from invoices inner join orders on orders.invoice_id = invoices.id where invoices.deleted_at is null group by invoices.id
Then it's working fine means no mode validations trough mySQL it has configured side of Laravel
Also, i haven't found related database modes in laravel documentation
So help me out if I did wrong something.
And i also want to know who's database strict mode validation is enabled in laravel because I have wonder what strict validation giving me an error.
Change this line in mysql array of config/database.php
'strict' => true,
To
'strict' => false,
Where this occurs add this top of the function
public function index() {
DB::statement("SET sql_mode = '' ");
# rest of your Joins
}
run this query after setting connection in database file
SET sql_mode = false;
I dont want to believe there is a bug in laravel 5.5 cos i am amazed how i keep getting errors on the Eloquent Query Builder when ever i use the groupBy in my query.
Tried converting this SQL that works seamlessly to Eloquent and i keep getting Errors on the groupBy..
Had a similar issue with an SQL which i complained here and till now i have not gotten a perfect answer for it
open issue here
SELECT * FROM arm_articles
INNER JOIN arm_interest ON arm_articles.article_category = arm_interest.category_id
WHERE arm_articles.article_contributor_id='1322'
GROUP BY arm_articles.article_id
ELOQUENT VERSION THAT RETURNS ERROR
$contributor_id =1322;
DB::table('arm_articles')
->join('arm_interest', function($join) {
$join->on('arm_articles.article_category','=','arm_interest.category_id');
})
->having('arm_articles.article_contributor_id', '=', $contributor_id)
->groupBy('arm_articles.article_id')->get()
ERROR MESSAGE
SQLSTATE[42000]: Syntax error or access violation: 1055 'knowledge_db.arm_articles.id' isn't in GROUP BY (SQL: select * from 'arm_articles' inner join 'arm_interest' on 'arm_articles'.'article_category' = 'arm_interest'.'category_id' group by 'arm_articles'.'article_id' having 'arm_articles'.'article_contributor_id' = 1322)
So like i said b4 its really weared why i will get the error in the first place as adding ->toSql to the query returns the right SQL.
Add this configuration to your mysql database config in config/database.php:
'mysql' => [
....
'strict' => false,
//'strict' => true,
],
Edit sql_mode in your mysql config file (my.ini or my.cnf)
[mysqld]
sql_mode = STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION
Restart your MySQL
I have tried to get data from database with laravel Where JSON. But it gives an error. I have added the data like this.
$event = new Event;
$event->scores = json_encode([['user_ids' => [1,2,3,4],'score' => 15]]);
$event->save();
When I want to return the data in database.
Event::where('scores->score',15)->get()
Shows this error:
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an
error in your SQL syntax; check the manual that corresponds to your
MariaDB server version for the right syntax to use near '>'$."score"'
= ?' at line 1 (SQL: select * from `events` where `scores`->'$."score"' = 15)
My MariaDB version is 10.2.1
Array & JSON Casting
On your Event model you need to add
class Event extends Model {
protected $casts = ['scores'=>'array'];
// ...
}
and then when saving the data
$event = new Event;
// you had what appeared to be an extra array, that could cause the issue?
$event->scores = ['user_ids' => [1,2,3,4],'score' => 15];
$event->save();
Then the data will be saved as JSON automatically as long as the column type is TEXT or JSON(new versions of MySQL).
See Array & JSON casting on the Laravel Documentation: https://laravel.com/docs/5.5/eloquent-mutators#array-and-json-casting
Then to retrieve:
Event::where('scores->score', '15')->get();
Here is the relevant documentation for JSON Where clauses
https://laravel.com/docs/5.5/queries#json-where-clauses
Again, pretty confused as to what you are asking. Might help to see structure of the table or what the expected SQL should look like.
If you are trying to return rows based off values contained inside a string of JSON stored on the table... Then you need to do something like...
->whereRaw("JSON_CONTAINS(score, '[15]' )")->get();
Or something along those lines... https://dev.mysql.com/doc/refman/5.7/en/json-search-functions.html
im using symfony with doctrine, and Im trying to generate the schema yml file from a db. I get an this error
SQLSTATE[42000]: Syntax error or access violation: 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 'group' at line 1. Failing Query: "DESCRIBE group"I have a table named group in my database, and I suspect that this is screwing it up.
Its not access problems as ive checked my db previleges. Any suggestions on what I can do? I have tried the quote-identifier attribute but with no luck : (
Im also struggling to find some good doctrine documentation. i cant seem to find where a list of attributes are, say for a creating a column in the schema.yml file. I tried reaching out to symfony forums but they are not responsive! Any help would be greatly appreciated! Thanks..
As mentioned group is reserved keyword in MySQL, so you need to escape its name. In your project configuration class (/config/ProjectConfiguration.class.php) configure Doctrine Manager to use quotes:
class ProjectConfiguration extends sfProjectConfiguration {
public function setup() {
//...
}
public function configureDoctrine(Doctrine_Manager $manager) {
$manager->setAttribute(Doctrine_Core::ATTR_QUOTE_IDENTIFIER, true);
}
}
Assuming you're using up-to-date MySQL, "group" is a reserved word:
http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html