Symfony doctrine::build task cannot create table named order - mysql

I have a database with a table named order.
When i run php symfony doctrine:build --all, i got the folowing 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 'order (id BIGINT
AUTO_INCREMENT, status VARCHAR(255),
colissimonumber VARCHAR(25' at line 1.
Failing Query: "CREATE TABLE order (id
BIGINT AUTO_INCREMENT, status
VARCHAR(255), colissimonumber
VARCHAR(255) NOT NULL, created_at
DATETIME NOT NULL, updated_at DATETIME
NOT NULL, PRIMARY KEY(id)) ENGINE =
MyISAM".
The problem is clearly that order has no backquotes arount it (if i run manually the query in phpmyadmin with backquotes, it works)
How do i tell doctrine to add backquotes around table and field names? Any workaround exept renaming my table ?
I run symfony 1.4.9 with doctrine 1.2

You can turn on Doctrine_Core::ATTR_QUOTE_IDENTIFIER in your doctrine configuration mthod on projectConfiguration which will quote tables and col names but its not recommended:
Just because you CAN use delimited
identifiers, it doesn't mean you
SHOULD use them. In general, they end
up causing way more problems than they
solve. Anyway, it may be necessary
when you have a reserved word as a
field name (in this case, we suggest
you to change it, if you can).
http://www.doctrine-project.org/projects/orm/1.2/docs/manual/configuration/en#identifier-quoting

You probably want your model to be named Order, but this doesn't mean that the corresponding RDBMS table must be named the same.
Order:
tableName: project_order
columns: ...

Got similar error when upgrading from MySQL 5.7 to MySQL 8.0. RANK() is a function added in MySQL 8.0 whereas one of the table in our database has rank as column name.
My Setup:
- Symfony 1.5
- Doctrine 1.2
- PHP 7.4.24
- MySQL 8.0.27
- Ubuntu 20.04.1
Here are three possible solutions.
Solution 1: Add quote_identifier: true in config/databases.yml file. Also, clear cache after change in configuration with php symfony cc or php symfony cache:clear.
all:
doctrine:
param:
attributes:
quote_identifier: true
Solution 2: Turn on Doctrine_Core::ATTR_QUOTE_IDENTIFIER in config/ProjectConfiguration.class.php file on configureDoctrine() method.
$conn = Doctrine_Manager::getInstance()->getCurrentConnection();
$conn->setAttribute(Doctrine_Core::ATTR_QUOTE_IDENTIFIER, true);
Solution 3: Turn on Doctrine_Core::ATTR_QUOTE_IDENTIFIER on specific table(s) which are potentially breaking the system.
$table = Doctrine_Core::getTable('table_name');
$table->setAttribute(Doctrine_Core::ATTR_QUOTE_IDENTIFIER, true);
Note: From Doctrine 1 docs
Just because you CAN use delimited identifiers, it doesn't mean you
SHOULD use them. In general, they end up causing way more problems
than they solve. Anyway, it may be necessary when you have a reserved
word as a field name (in this case, we suggest you to change it, if
you can).

Related

MySql to Hsqldb migration with engine=InnoDb in sql

I am creating a spring profile for dynamic environments in gitlab and don't want to create a separate mysql db for each instance, so what I try is to use my existing liquibase migrations with hsqldb in that speciffic profile, which seems to work besides the engine=InnoDb part in the sql.
I already added sql.syntax_mys=true to the datasource url, which supported the datatypes, not the engine part tho.
Since I want to avoid writing different sql migrations for the dynamic environments and already have a prod instance changing the migration or adding separate migrations is not really an option for me.
Is there a way to tell hsql to just ignore that part, or define it as some function which does nothing?
An example sql would be:
create table if not exists xy(
field1 varchar(255) not null,
field2 ....
) engine=InnoDB;
MySQL supports comments, including a special format for conditional execution:
https://dev.mysql.com/doc/refman/8.0/en/comments.html
If you add a version number after the ! character, the syntax within the comment is executed only if the MySQL version is greater than or equal to the specified version number. The KEY_BLOCK_SIZE keyword in the following comment is executed only by servers from MySQL 5.1.10 or higher:
CREATE TABLE t1(a INT, KEY (a)) /*!50110 KEY_BLOCK_SIZE=1024 */;
HSQLDB also supports comment syntax in SQL statements: http://www.hsqldb.org/doc/1.8/guide/ch09.html#N124ED
All these types of comments are ignored by the database.
Based on this, you could put the ENGINE=InnoDB into a comment so that HSQLDB will ignore it, but MySQL will run it:
create table if not exists xy(
field1 varchar(255) not null,
field2 ....
) /*!10000 engine=InnoDB; */
An automatic stripping feature will be added to HSQLDB in the next version.
In the meantime, you could modify the source code of JDBCStatement to check and strip the string when it is submitted.
Update: A snapshot jar with this feature is now available at http://hsqldb.org/download

MySQL schema name with dash causes errors for Hibernate hbm2ddl.auto

I'm working with hibernate.hbm2ddl.auto=update in development environments and recently I've upgraded hibernate version from 5.1.0.Final to 5.2.X.Final and I've started to experience errors in hibernate's auto generated commands for my schema called portal-appname.
In hibernate 5.1.0 when I add a new column for a given table the following command its executed:
Hibernate: alter table answer add column fake integer not null
But in Hibernate 5.2.X, the schema is added as a prefix to the given tablename:
Hibernate: alter table portal-appname.answer add column fake integer not null
This is not a valid sql command, obviously:
MySQL server version for the right syntax to use near '-appname.answer add column fake integer not null' at line 1
Hibernate should enclose portal-appname.table with backtips automatically as:
Hibernate: alter table `portal-appname`.`answer` add column fake integer not null
I've tried with hibernate.globally_quoted_identifiers but it only quotes column names but not the portal-appname.table pair.
Funny thing is that hibernate 5.2.X is only using that syntax with column names, but it's not prefixing the schema in other kind of alters such as:
Hibernate: alter table tablename add constraint FKftsiakun1f5qp01aabdw887kp foreign key (logo) references tablename2 (id)
Last but not least, I'm in moment where I'm able to rename my schema to other thing. Also, I can downgrade to hibernate 5.1.0 but I want to know why hibernate has introduced this behaviour and if there's something to avoid it.
Any help is much welcome.
To keep Hibernate 5.2+, you have to disable the shema qualifier in the dialect :
#Override
public NameQualifierSupport getNameQualifierSupport() {
return NameQualifierSupport.NONE;
}
Sample dialect there.
Also be sure to use a specific user able to see only the target database: do not use "root". Hibernate fetches all visible tables from all databases even if different from the one you specified.

Asigning 2 integer values to a single item in a table in MySQL?

I have this line in an SQL table:
Income NUMBER(12,2),
And I want to recreate the table in mysql, but it's throwing me this error:
error 1064 (42000): You have an error in your SQL syntax; check the manual to use near '2))' at line 4
This is how I'm creating the table:
CREATE TABLE Doctors(
DoctorId INTEGER(5) PRIMARY KEY,
DoctorName VARCHAR(20),
Income INTEGER(12,2));
How can I do this in mysql?
*I have been able to replicate the table in sqlite without errors.
If you are trying to do what your question title states (i.e. store two integer values into the same column), you may be looking to use an array. Databases like PostgreSQL support arrays (see PostgreSQL array documentation), but MySQL doesn't do so natively (see MySQL Documentation). However, the MySQL documentation suggests using a language extension to serialise/deserialise the data (see PHP example).
Otherwise, it may be that you are simply looking for a DECIMAL type:
Income DECIMAL(12,2)
See MySQL Documentation for fixed-point types

converted sqlite db wont import with phpmyadmin

Converted sqlite3 to mysql, but the import fails in phpMyAdmin and produces this error: #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 'PRIMARY KEY NOT NULL ,sequence VARCHAR,items_count INTEGER,total FLOAT,`c' at line 1
What am I missing?
DROP TABLE IF EXISTS `orders`;
CREATE TABLE `orders` (`id` VARCHAR PRIMARY KEY NOT NULL ,`sequence` VARCHAR,`items_count` INTEGER,`total` FLOAT,`change` FLOAT,`tax_subtotal` FLOAT,`surcharge_subtotal` FLOAT,`discount_subtotal` FLOAT,`payment_subtotal` FLOAT,`rounding_prices` VARCHAR,`precision_prices` VARCHAR,`rounding_taxes` VARCHAR,`precision_taxes` VARCHAR,`status` INTEGER,`service_clerk` VARCHAR,`service_clerk_displayname` VARCHAR,`proceeds_clerk` VARCHAR,`proceeds_clerk_displayname` VARCHAR,`member` VARCHAR,`member_displayname` VARCHAR,`member_email` VARCHAR,`member_cellphone` VARCHAR,`invoice_type` VARCHAR,`invoice_title` VARCHAR,`invoice_no` VARCHAR,`invoice_count` INTEGER,`destination` VARCHAR,`table_no` INTEGER,`check_no` INTEGER,`no_of_customers` INTEGER,......
There is a litte differnce between SQLite and MySQL syntax.
Below answer is taken from here
Here a list of ALL the differences in SQL syntax that I know about between the two file formats: The lines starting with:
BEGIN TRANSACTION
COMMIT
sqlite_sequence
CREATE UNIQUE INDEX
are not used in MySQL
SQLlite uses CREATE TABLE/INSERT INTO "table_name" and MySQL uses CREATE TABLE/INSERT INTO table_name
MySQL doesn't use quotes inside the schema definition
MySQL uses single quotes for strings inside the INSERT INTO clauses
SQLlite and MySQL have different ways of escaping strings inside INSERT INTO clauses
SQLlite uses 't' and 'f' for booleans, MySQL uses 1 and 0 (a simple regex for this can fail when you have a string like: 'I do, you don\'t' inside your INSERT INTO)
SQLLite uses AUTOINCREMENT, MySQL uses AUTO_INCREMENT
The above link will help more regarding this.

MySQL Creating temporary table syntax error

I am getting a syntax error when I run a MySQL statement.
I know backticks can be added to the tablename to make it work but was wondering why it happens in the first place.
Create temporary table 6514202534e1b20f0d6331 (user_id INT (10)) ENGINE=HEAP;
If I put this in Mysql Query Browser it treats the table name as two seperate words - 6514202534e1 and b20f0d6331.
The table name is generated dynamically and I haven't had a problem with this before, so I was wondering why it stopped working all of a sudden.
I think this is because the server (mysql) understands it in this case as 6514202534*e^1 INT.
Using the ` character:
CREATE TEMPORARY TABLE `6514202534e1b20f0d6331` (user_id INT (10)) ENGINE=HEAP;
In this way the MySQL Server understands (explicitly) that the whole phrase is database, table or field not a value or function, etc.
For example `order` is legal while just order will rise an error for invalid order clause.