symfony - configure postgres entity manager - postgresql

I would like to change the database in my existing project from mysql to postgresql.
I have configured the database, I have regenerated the migrations which work, but the problem appears in the fixtures.
when trying to load fixtures an error appears like this:
An exception occurred while executing 'INSERT INTO user (nickname, password, id, created_at, updated_at, email) VALUES (?, ?, ?, ?, ?, ?)' with params ["user", "$2y$13$rgHtT56Vlk2
avmf3gX2W7.QYcQ5d6AXRzr41ebRMGfxREqLZQfsTG", "017c4562-d487-ddff-c303-108c1916d6dd", "2021-10-03 11:01:16", "2021-10-03 11:01:16", "user#user.pl"]:
SQLSTATE[42601]: Syntax error: 7 BŁĄD: błąd składni w lub blisko "user"
LINE 1: INSERT INTO user (nickname, password, id, created_at, update... ,
this is a problem possibly caused by entity manager generating the mysql dialect instead of postgres dialect.
a similar error occurs during the get shot under the user entity:
"hydra:description": "An exception occurred while executing 'SELECT u0_.nickname AS nickname_0, u0_.password AS password_1, u0_.id AS id_2, u0_.created_at AS created_at_3, u0_.updated_at AS updated_at_4, u0_.email AS email_5 FROM user u0_':\n\nSQLSTATE[42703]: Undefined column: 7 BŁĄD: kolumna u0_.nickname nie istnieje\nLINE 1: SELECT u0_.nickname AS nickname_0, u0_.password AS password_.
Here is my doctrine.yaml configuration:
doctrine:
dbal:
url: '%env(resolve:DATABASE_URL)%'
driver: 'pdo_pgsql'
charset: utf8
# IMPORTANT: You MUST configure your server version,
# either here or in the DATABASE_URL env var (see .env file)
#server_version: '13'
orm:
auto_generate_proxy_classes: true
naming_strategy: doctrine.orm.naming_strategy.underscore_number_aware
auto_mapping: true
mappings:
App:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Entity'
prefix: 'App\Entity'
alias: App
Could someone help me get rid of the bug? :)

Related

DB2 select JSON_ARRAYAGG

Using of JSON_ARRAYAGG does not working for me
1) [Code: -104, SQL State: 42601] An unexpected token "ORDER" was found following "CITY)
". Expected tokens may include: ")".. SQLCODE=-104, SQLSTATE=42601, DRIVER=4.28.11
2) [Code: -727, SQL State: 56098] An error occurred during implicit system action type "2". Information returned for the error includes SQLCODE "-104", SQLSTATE "42601" and message tokens "ORDER|CITY)
|)".. SQLCODE=-727, SQLSTATE=56098, DRIVER=4.28.11
I want an output like this
{"id":901, "name": "Hansi", "addresses" :
[
{ "address":"A", "city":"B"},
{ "address":"C", "city":"D"}
]
}
I am using IBM DB2 11.1 for Linux, UNIX and Windows.
values (
json_array(
select json_object ('ID' value ID,
'NAME' value NAME,
'ADDRESSES' VALUE JSON_ARRAYAGG(
JSON_OBJECT('ADDRESS' VALUE ADDRESS,
'CITY' VALUE CITY)
ORDER BY ADDRESS)
)
FROM CUSTOMER
JOIN CUSTOMER_ADDRESS ON ADDRESS_CUSTOMER_ID = ID
GROUP BY ID, NAME
FORMAT JSON
));
Used tables are:
CUSTOMER - ID (INT), NAME (VARCHAR64)
ADDRESS - ADDRESS (VARCHAR64), CITY (VARCHAR64)

Knex cannot find table in Cloud SQL Postgres from Cloud Functions

I am trying to connect to a Postgres 12 DB running in Cloud SQL from a Cloud Function written in TypeScript.
I create the database with the following:
import * as Knex from "knex"
const { username, password, instance } = ... // username, password, connection name (<app-name>:<region>:<database>)
const config = {
client: 'pg',
connection: {
user: username,
password: password,
database: 'ingredients',
host: `/cloudsql/${instance}`,
pool: { min: 1, max: 1}
}
}
const knex = Knex(config as Knex.Config)
I am then querying the database using:
const query = ... // passed in as param
const result = await knex('tableName').where('name', 'ilike', query).select('*')
When I run this code, I get the following error in the Cloud Functions logs:
Unhandled error { error: select * from "tableName" where "name" ilike $1 - relation "tableName" does not exist
at Parser.parseErrorMessage (/workspace/node_modules/pg-protocol/dist/parser.js:278:15)
at Parser.handlePacket (/workspace/node_modules/pg-protocol/dist/parser.js:126:29)
at Parser.parse (/workspace/node_modules/pg-protocol/dist/parser.js:39:38)
at Socket.stream.on (/workspace/node_modules/pg-protocol/dist/index.js:10:42)
at Socket.emit (events.js:198:13)
at Socket.EventEmitter.emit (domain.js:448:20)
at addChunk (_stream_readable.js:288:12)
at readableAddChunk (_stream_readable.js:269:11)
at Socket.Readable.push (_stream_readable.js:224:10)
at Pipe.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)
I created the table using the following commands in the GCP Cloud Shell (then populated with a data from a CSV):
\connect ingredients;
CREATE TABLE tableName (name VARCHAR(255), otherField VARCHAR(255), ... );
In that console, if I run the query SELECT * FROM tableName;, I see the correct data listed.
Why does Knex not see the table: tableName, but the GCP Cloud Shell does?
BTW, I am definitely connecting to the correct db, as I see the same error logs in the Cloud SQL logging interface.
Looks like you are creating the table tableName without quoting, which makes it actually lower case (case insensitive). So when creating schema do:
CREATE TABLE "tableName" ("name" VARCHAR(255), "otherField" VARCHAR(255), ... );
or use only lower-case table / column names.

Unknown database type json requested, Doctrine\DBAL\Platforms\PostgreSqlPlatform may not support it

Developing using Symfony 2.7
I have entity which contain attribute
/**
* #var array
* #ORM\Column(name="new_entry_name", type="json_array", nullable=true)
*/
protected $newEntryName;
but when i update my schema using
php app/console doctrine:schema:update --force
it shows me error
$ php app/console doctrine:schema:update --force
[Doctrine\DBAL\DBALException]
Unknown database type json requested, Doctrine\DBAL\Platforms\PostgreSqlPlatform may not support it.
In config.yml file i have added this type.
doctrine:
dbal:
driver: "pdo_pgsql"
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
password: "%database_password%"
charset: UTF8
mapping_types:
enum: string
set: string
varbinary: string
tinyblob: text
types:
json: Sonata\Doctrine\Types\JsonType
what should i do to avoid this error .Thanks
To avoid this error add
json: json_array
or
json: json
in config.yml under mapping_types section.
So, mapping_types section should look like this:
mapping_types:
enum: string
set: string
varbinary: string
tinyblob: text
json: json_array

symfony2 propel-bundle + postgres: syntax error near "."

this is for my login form which uses propel as user provider, I get this error which I can understand from, that identifiers are not quoted:
and error:
Unable to execute SELECT statement [SELECT user.id, user.username, user.password, user.email, user.type, user.first_name, user.last_name, user.national_code, user.personal_code, user.role, user.card, user.university_id, user.salt, user.active, user.created_at, user.updated_at FROM user WHERE user.username=:p1 LIMIT 1] [wrapped: SQLSTATE[42601]: Syntax error: 7 ERROR: syntax error at or near "." LINE 1: SELECT user.id, user.username, user.password, user.email, us... ^]
my propel configuration:
propel:
path: "%kernel.root_dir%/../vendor/propel"
phing_path: "%kernel.root_dir%/../vendor/phing"
logging: %kernel.debug%
dbal:
default_connection: default
connections:
default:
driver: %database_driver%
user: %database_user%
password: %database_password%
dsn: %database_driver%:host=%database_host%;dbname=%database_name%
options:
ATTR_PERSISTENT: false
attributes:
ATTR_EMULATE_PREPARES: true
settings:
charset: { value: UTF8 }
build_properties:
propel.database: %database_driver%
propel.database.url: ${propel.dsn}
propel.database.buildUrl: ${propel.database.url}
propel.database.createUrl: ${propel.database.buildUrl}
propel.database.user: %database_user%
propel.database.password: %database_password%
propel.platform.class: platform.${propel.database}Platform
propel.disableIdentifierQuoting: false
and also my provider config:
providers:
main:
propel:
class: National\PublicationBundle\Model\User
property: username
is something wrong with my config? I also found this on github and changed my code accordingly but it did change nothing, what I did:
\$sql = sprintf(
'$query',
implode(', ', array_map(function(\$e) { return '\"'.\$e.'\"' ; }, \$modifiedColumns)),
implode(', ', array_keys(\$modifiedColumns))
);
one more thing: when I generate sql wih php app/console propel:build:sql, sql codes are quoted correctly.
answer: user is a reserved word! so just changed it to users and solved! :\

symfony2 session storage with postgresql

I tried to store my session data into a postgresql database.
I have to precise that with the default session storage i've got any problem.
So i configure my config file as explained here :
http://symfony.com/doc/current/cookbook/configuration/pdo_session_storage.html
session:
handler_id: session.handler.pdo
parameters:
pdo.db_options:
db_table: session
db_id_col: session_id
db_data_col: session_value
db_time_col: session_time
pdo:
class: PDO
arguments:
- "pgsql:dbname=%database_name%"
- %database_user%
- %database_password%
session.handler.pdo:
class: Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler
arguments: [#pdo, %pdo.db_options%]
and then when i clear the cache i got this error :
[PDOException]
SQLSTATE[08006] [7] FATAL: authentification peer ?chou?e pour l'utilisateur
here is my script used to create table session :
CREATE TABLE session
(
session_id character varying(255) NOT NULL,
session_value text NOT NULL,
session_time integer NOT NULL,
CONSTRAINT session_pkey PRIMARY KEY (session_id )
)
WITH (
OIDS=FALSE
);
ALTER TABLE session
OWNER TO myuser;
can't Anyone to help me ?
The PDO dsn is missing the host :
session:
handler_id: session.handler.pdo
parameters:
pdo.db_options:
db_table: session
db_id_col: session_id
db_data_col: session_value
db_time_col: session_time
pdo:
class: PDO
arguments:
- "pgsql:host=%database_host%;dbname=%database_name%"
- %database_user%
- %database_password%
session.handler.pdo:
class: Symfony\Component\HttpFoundation\Session\Storage\Handler\PdoSessionHandler
arguments: [#pdo, %pdo.db_options%]
Without the host value, i had the same error as you :
[PDOException]
SQLSTATE[08006] [7] FATAL: Peer authentication failed for user "postgres"
Hope this helps