I have an account entity which contains a foreign key constraint for a user entity and customer entity.
I'm trying to validate the customer id is numeric and not null. Plus the unique constraint of user_id and customer_id.
My problem the Class Constraint UniqueEntity gets fired regardless of the outcome of the customer_id type: numeric validation result. Which throws a 500 because the customer_id column is an integer.
What I need to do is, validate the customer_id is a valid id (numeric and not null) before checking the unique constraint. Or is there any other way to avoid symfony/doctrine going to the db to test an invalid id?
My validation config,
MY\ApplicationBundle\Entity\Account:
constraints:
- Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity:
fields: [ user_id, customer_id ]
message: Customer Id already exists for User
properties:
customer_id:
- Type:
type: numeric
message: Customer Id should be an integer.
- NotBlank:
message: Customer cannot be blank.
Group Sequences should solve your problem here. Something like that should work:
MY\ApplicationBundle\Entity\Account:
group_sequence:
- Basic
- Account
constraints:
- Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity:
fields: [ user_id, customer_id ]
message: Customer Id already exists for User
properties:
customer_id:
- Type:
type: numeric
message: Customer Id should be an integer.
groups: [Basic]
- NotBlank:
message: Customer cannot be blank.
groups: [Basic]
With group sequences you can define the order of your validations. Basic checks all constraints with the group Basic and Account cheks the other constraints for this entity.
Related
I run into this issue:
Caused by: java.lang.IllegalArgumentException:
Error: User error in query:
CREATE TABLE failed for table TEST:
Table exists but definitions do not match
I added 2 new columns in my existing database using console, and added them in table creation statement. It hits the error when I run creating table again.
I checked everything, they all match, (name, type…)
the order of columns matter when you create a table. Did you validate this ? The IF NOT EXISTS in the CREATE TABLE clause checks for the same structure of the table. Please refer the documentation below for more details.https://docs.oracle.com/en/database/other-databases/nosql-database/21.1/sqlreferencefornosql/create-table.html
This inform you that the table that you are trying to create exits but the structure is not the same. It could be useful.
A sample to show the error when order of columns is changed:
sql-> CREATE TABLE IF NOT EXISTS testing_tbl(id INTEGER, firstName STRING,
lastName STRING,
PRIMARY KEY (id));
Statement completed successfully
sql-> CREATE TABLE IF NOT EXISTS testing_tbl(id INTEGER, firstName STRING,
lastName STRING,
PRIMARY KEY (id));
Statement did not require execution
sql-> CREATE TABLE IF NOT EXISTS testing_tbl(id INTEGER, lastName STRING,
firstName STRING,
PRIMARY KEY (id));
Error handling command CREATE TABLE IF NOT EXISTS testing_tbl(id INTEGER,
lastName STRING,
firstName STRING,
PRIMARY KEY (id)): Error: User error in query: CREATE TABLE failed for table testing_tbl: Table exists but definitions do not match
I have a ENUM column and I am using liquibase addCheckConstraint to allow only ENUM values. However, addCheckConstraint doesnt work and allows any value. My yaml changelog looks as follows:
- changeSet:
id: 1565352995028-1
author: Zafrullah Syed
changes:
- createTable:
columns:
- column:
constraints:
primaryKey: true
name: ID
type: NUMBER(19, 0)
- column:
name: STATUS_ENUM
type: VARCHAR2(10 CHAR)
tableName: BOOKING
- addCheckConstraint:
constraintBody: STATUS_ENUM IN ('ERROR', 'CONFIRMED', 'QUEUED', 'REJECTED')
constraintName: enumcheck
tableName: SOME_TABLE
I am Using Oracle db.
If I try to run a sql query, then constraint works well:
ALTER TABLE SOME_TABLE
ADD CONSTRAINT enumcheck CHECK (status_enum IN ('ERROR', 'CONFIRMED', 'QUEUED', 'REJECTED'));
I tried to add another enableCheckConstraint to enable the existing check constraint but nothing seems to work.
addCheckConstraint is accessible only for the pro version, do you have it?
Problem: I have a table categories on my database which contains the cat_id as primary key of type int and cat_name as name of the category. But I collect info from the under via radio.
Question: How can I convert the 'on' and 'off' from the user to integers?
Error Given:
Illuminate\Database\QueryException SQLSTATE[HY000]: General error:
1366 Incorrect integer value: 'on' for column 'type' at row 1 (SQL:
insert into users (user_name, name, type, email, password,
updated_at, created_at) values (malico, John Doe, on,
john.doe#gmail.com,
$2y$10$yyf4Vt01ukTNPFxGa1PIheJZvmcPbozGjdz8zo84vkK325Qk6rfna,
2018-05-19 12:56:01, 2018-05-19 12:56:01))
just do something like this on your request :
$request->merge(['type'=>$request->get('type') == 'on' ? 1 : 0]);
this will change "on" to 1 in your request
Is there possible to do a hasManyThrough getting user's country id from a Many to Many pivot table (country_user) ? something like
countries
id - integer
name - string
users
id - integer
name - string
posts
id - integer
user_id - integer
title - string
country_user
user_id - integer
country_id - integer
I have the following table schema:
db.version(1).stores({
sales: "[item_id+date],sales"
});
Where the combination of date and item_id must be unique. How can I get all the records for a given item_id using the where clause (disregarding the date)?
The following produces an error:
db.sales.where('item_id').equals(some_item_id)
Unhandled rejection: SchemaError: KeyPath item_id on object store sales
is not indexed
db.sales.where('[item_id+date]').between ([some_item_id, -Infinity], [some_item_id, Dexie.maxKey])