There's something very very wrong with Doctrine/MySQL or I'm just completly dumb. I'm trying to execute a simple update query on Question entity:
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| form_id | int(11) | YES | MUL | NULL | |
| name | varchar(255) | NO | | NULL | |
| question_type | varchar(255) | NO | | NULL | |
| validation | longtext | YES | | NULL | |
| required | tinyint(1) | YES | | NULL | |
| order | int(11) | NO | | NULL | |
+---------------+--------------+------+-----+---------+----------------+
With:
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('order', IntegerType::class)
->add('name', TextType::class)
->add('questionType', TextType::class)
->add('validation', TextType::class)
->add('required', CheckboxType::class)
;
}
and
...
$em->persist($question);
...
I'm getting this error:
Uncaught PHP Exception Doctrine\DBAL\Exception\SyntaxErrorException: "An exception occurred while executing 'UPDATE questions SET order = ? WHERE id = ?' with params [1, 12]
Every other field validates with no problem!
When I'm trying to run the same query in the console, this is the result:
mysql> UPDATE questions SET order=1 WHERE id=14;
ERROR 1064 (42000): 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 'order=1 WHERE id=14' at line 1
When I run the same query in Workbench, everything is ok:
UPDATE `questions` SET `order`='2' WHERE `id`='7';
Finally, updating another column in this table (also integer!), like so:
update questions set form_id=5 where id=7;
Goes as expected.
What is going on?
EDIT:
To simplify, why this works:
UPDATE questions SET form_id=5 WHERE id=7;
And this doesn't:
UPDATE questions SET order=3 WHERE id=7;
order is a reserved word for Mysql
That's why it runs when (escaped with backticks)
It goes without saying that choosing another name would be safer
See this answer for more informations
Related
I cannot change databases column
My Env
MacOS Mojave, MySQL Server version: 10.1.39-MariaDB Source distribution
why
Making a CRUD app, but I want to change table column,
from text to desc, so I searched and used alter
command, but right SQL command returns error messages.
My table
MariaDB [cake_cms]> describe interns;
+----------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| email | varchar(255) | NO | | NULL | |
| name | varchar(64) | NO | | NULL | |
| text | varchar(255) | NO | | NULL | |
| location | varchar(64) | YES | | NULL | |
+----------+--------------+------+-----+---------+----------------+
MariaDB [cake_cms]> Alter Table interns Rename Column text to desc;
ERROR 1064 (42000): 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 'Column text to desc' at line 1
Refered
https://www.dbonline.jp/mysql/table/index18.html
says to use
ALTER TABLE table_name
CHANGE COLUMN old_name TO new_name;
Rename a column in MySQL
This site says:
ALTER TABLE tableName RENAME COLUMN "oldcolname" TO "newcolname" datatype(length);
So I write
alter table interns rename column "name" to "newname" varchar(255);
But returned syntax error message....
I do not know what to do. Please help me!
desc is a sql command so you can't name your table like this
I'm struggling with a simple update command to a row in a table called lychee_settings, table description is:-
+-------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+-------+
| key | varchar(50) | NO | | | |
| value | varchar(200) | YES | | | |
+-------+--------------+------+-----+---------+-------+
row I want to change is (0 to 1):-
| skipDuplicates | 0 |
I am running
UPDATE lychee_settings SET value = '1' WHERE key = 'skipDuplicates';
which returns an
You have an error in your SQL syntax.
I can't see what I am doing wrong, must be something very simple, any help much appreciated!
key is a reserved word in MySQL so if you have to use it as a column name (not recommended) you have to wrap that column name is backticks.
UPDATE lychee_settings SET value = '1' WHERE `key` = 'skipDuplicates';
Working on mysql.5.7
Here is my bugs table
MySQL [jira_statistics]> describe bugs;
+---------------------------------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------------------------------+--------------+------+-----+---------+-------+
| issue_key | varchar(45) | NO | PRI | NULL | |
| release_name | varchar(45) | YES | MUL | NULL | |
| issue_summary | varchar(200) | YES | | NULL | |
| story_points | int(11) | NO | | 0 | |
| qa_reopened | float | NO | | 0 | |
| done_reopened | float | NO | | 0 | |
This table is updated by periodic calls to LOAD DATA LOCAL INFILE bugs <file.csv>
Whenever this update takes place (which may either update existing lines and/or insert new ones) I want another table that has some yielded statistics to be updated via the following trigger
create trigger update_bugs_stats after insert on `jira_statistics`.`bugs` for each row
begin
delimiter ;
-- STORY POINTS -------------------------
SELECT AVG(story_points) INTO #avg_bugs_storypoints FROM `jira_statistics`.`bugs` WHERE release_name = new.release_name;
SELECT MAX(story_points) INTO #max_bugs_storypoints FROM `jira_statistics`.`bugs` WHERE release_name = new.release_name;
SELECT MIN(story_points) INTO #min_bugs_storypoints FROM `jira_statistics`.`bugs` WHERE release_name = new.release_name;
INSERT INTO storypoints_stats (release_name, avg_bugs_storypoints, max_bugs_storypoints, min_bugs_storypoints)
VALUES (relName, #avg_bugs_storypoints, #max_bugs_storypoints, #min_bugs_storypoints)
ON DUPLICATE KEY UPDATE
relName=new.release_name,
avg_bugs_storypoints=#avg_bugs_storypoints,
max_bugs_storypoints=#max_bugs_storypoints,
min_bugs_storypoints=#min_bugs_storypoints;
However this gives me the following error whenever trying to create the trigger:
Unknown column new.release_name in where clause.
Why isn't the new keyword bein recognized?
Because new is reserved as a system word
Ref: https://dev.mysql.com/doc/refman/8.0/en/keywords.html
Please modify
new.release_name ==> `new`.`release_name`
etc..
Τhe error was more stupid than I thought;
I was working directly on sql query editor and not on the triggers tab of mysql workbench so it did not parse correctly the new keyword`.
I have an update query that shouldbe working but for some reason it doesnt work
String sql="UPDATE TB_EARTHORIENTATIONPARAMETER_UI SET YEAR='year1', MONTH='month1', DAY='day1', MJD='mjd1', WHERE (EOPID=1)";
It gives me the following error
Incorrect integer value 'year1' for column YEAR at row1
my table consist of the following columns and their types
| EOPID | int(11) | NO | PRI | NULL | auto_increment |
| YEAR | int(11) | YES | | NULL | |
| MONTH | int(11) | YES | | NULL | |
| DAY | int(11) | YES | | NULL | |
| MJD | int(11) | YES | | NULL | |
I retrieve the valuues to use in my sql update query from a jTable in the following manner
Object year=model.getValueAt(row, column);
years=year.toString();
year1=Integer.parseInt(years);
so i believe i am using the correct type but i cant figure out why it wont update . Is this a mysql version thing?
Your query should be like.
String sql="UPDATE TB_EARTHORIENTATIONPARAMETER_UI
SET
YEAR="+year1+",
MONTH="+month1+",
DAY="+day1+",
MJD="+mjd1+"
WHERE
EOPID=1";
Where year1, month1, day1, mjd1 should be variables containing appropriate values (there is an extra comm before the WHERE clause though).
The system is complaining that you're giving it a STRING ("year1"), not the integer value (e.g. 2012) it's expecting.
You should write this more like:
String sql="UPDATE TB_EARTHORIENTATIONPARAMETER_UI SET YEAR=" +
year1.toString() + ", month..."
Can some one point out what I am doing wrong. I went through all related questioin on this site, but I think my problem is more with MYSQL, As the error mentioned.The problem is when I try to update a relationship between two tables using EF in asp.net mvc2.
I have these tables and I am using MYSQL.
Course
+------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------+----------------+
| courseid | int(10) unsigned | NO | PRI | NULL | auto_increment |
| coursename | varchar(100) | YES | | NULL | |
+------------+------------------+------+-----+---------+----------------+
Faculty:
+-----------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+------------------+------+-----+---------+----------------+
| facultyid | int(10) unsigned | NO | PRI | NULL | auto_increment |
| firstname | varchar(100) | YES | | NULL | |
| lastname | varchar(100) | YES | | NULL | |
+-----------+------------------+------+-----+---------+----------------+
And I have this table to create the relationship
+-----------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+------------------+------+-----+---------+-------+
| courseid | int(10) unsigned | NO | MUL | NULL | |
| facultyid | int(10) unsigned | NO | MUL | NULL | |
+-----------+------------------+------+-----+---------+-------+
Now when I want to associate a course to a faculty, both the course and faculty are existing, using the code below I got a MYSQL error: Below
[AcceptVerbs("POST")]
public ActionResult AddCourse(int id, FormCollection collection)
{
//I made this static values for debuging
Course course = scheduleEnt.Courses1.First(p => p.courseid == 5); //existing course
Faculty faculty = scheduleEnt.Faculties1.First(p => p.facultyid == 1); //existing faculty
faculty.courses.Add(course);
UpdateModel(faculty);
scheduleEnt.SaveChanges();//This is where the error generated
var cc = 5;
return RedirectToAction("AddCourse", new { cid = cc });
}
This is the error:
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 '(SELECT
`course_faculty`.`courseid`,
`course_faculty`.`facultyid`
' at line 1
OK, it turns out that the issue in my case was the fact the linking table enabling the n:m relationship didn't explicitly have a primary key set. So although the only two fields were both FKs, the DBA hadn't set them to be a primary key in their own right.
I found that if I looked within the source of the edmx under the ssdl section there were elements that matched the error I was getting when trying to perform the update:
<EntitySet Name="x_map" EntityType="DBModel.Store.x_map" store:Type="Tables" store:Schema="xxx" store:Name="x_map">
<DefiningQuery>SELECT
`xxx`.`xxx`,
`xxx`.`xxx`
FROM `xxx` AS `xxx`</DefiningQuery>
</EntitySet>
And then above the defining EntityType
<!--Errors Found During Generation:
warning 6002: The table/view 'xxx.x_map' does not have a primary key defined. The key has been inferred and the definition was created as a read-only table/view.
-->
Changing the DB schema to have the correct PKs and updating the the EDMX fixed the issue in my case.
Hope this goes some way towards you fixing your issue.