Customizing fixtures suites - sylius

I created a new fixtures which looks like the default one, when I execute the command bin/console sylius:fixtures:load I have an error does not occur all the time:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'name' cannot be null
INSERT INTO sylius_product_translation
(name, slug, description, meta_keywords, meta_description, short_description, locale, translatable_id)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
Input param values:
[null, null, null, null, null, null, "en_US", 470]

You're creating an empty product translation.
If you could share the fixture code, I could point you to the issue.

sylius_fixtures:
suites:
custom_default:
fixtures:
product_review:
options:
random: 20
similar_product_association:
options:
amount: 3
fulfilled_order:
name: order
options:
amount: 12
channel: 'My_CHANNEL'
fulfilled: true
new_order:
name: order
options:
amount: 8
channel: 'My_CHANNEL'
address:
options:
random: 4
prototype:
country_code: CN

Related

How to INSERT INTO table(column) VALUES(value) WHERE column2 = value

I am working on a Time Clock program but I am having trouble with this query:
INSERT INTO users(time_in) VALUES($2) WHERE username = $1
Table:
CREATE TABLE users (
id SERIAL PRIMARY KEY NOT NULL,
username VARCHAR(50) NOT NULL,
password VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
time_json VARCHAR,
time_in VARCHAR
);
Here is the error that I am getting:
error: syntax error at or near "time_in"
at Connection.parseE (\...\node_modules\pg\lib\connection.js:606:11)
at Connection.parseMessage (\...\node_modules\pg\lib\connection.js:403:19)
at TLSSocket.<anonymous> (\...\node_modules\pg\lib\connection.js:123:22)
at TLSSocket.emit (events.js:210:5)
at addChunk (_stream_readable.js:308:12)
at readableAddChunk (_stream_readable.js:289:11)
at TLSSocket.Readable.push (_stream_readable.js:223:10)
at TLSWrap.onStreamRead (internal/stream_base_commons.js:182:23) {
name: 'error',
length: 95,
severity: 'ERROR',
code: '42601',
detail: undefined,
hint: undefined,
position: '8',
internalPosition: undefined,
internalQuery: undefined,
where: undefined,
schema: undefined,
table: undefined,
column: undefined,
dataType: undefined,
constraint: undefined,
file: 'scan.l',
line: '1134',
routine: 'scanner_yyerror'}
Code for the function I am working on:
function postTimeIn(user, callback) {
const timeIn = getTime();
params = [user, timeIn];
const sql = 'INSERT INTO users(time_in) VALUES($2) WHERE username = $1';
pool.query(sql, params, function (err, result) { ...
The WHERE clause would make sense with an UPDATE:
UPDATE users
SET time_in = $2
WHERE username = $1;
Is that what you really want?
username should be defined UNIQUE for this - and probably in any case.
time_json should probably be type json or jsonb. time_in should probably be timestamptz.
An INSERT would not make any sense (apart from the invalid syntax), as you would have to fill in all NOT NULL columns without column default at the same time:
username VARCHAR(50) NOT NULL,
password VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
The problem is that you are trying to insert a record with a where condition. You can only use a where condition only as a filter when you are inserting values selected from another table
like.
INSERT INTO table(column1,column2,...)
SELECT column1,column2,...
FROM another_table
WHERE condition;
In your instant you should do an update as suggested above because you are changing the attribute value of an already existing record.

Sequelize Postgres - How to use ON CONFLICT for unique?

I am implementing sequelize into my NodeJS application. Before this, I was using a written INSERT query that used ON CONFLICT (field) DO NOTHING to handle not inserting records where a value needed to be unique.
const sql = 'INSERT INTO communications (firstname, lastname, age, department, campus, state, message_uuid) VALUES ($1, $2, $3, $4, $5, $6, $7) ON CONFLICT (message_uuid) DO NOTHING';
const values = [val.firstName, val.lastName, val.age, val.department, val.campus, val.state, message_uuid];
Is there support for this in sequelize where I can define the same thing within a model? Or perhaps a better way to handle it?
Essentially, if a record already exists in the table in the column with message_uuid = 123 and another record try's to insert that has that same value, it ignores it and does nothing.
You can use public static bulkCreate(records: Array, options: Object): Promise<Array> method with options.ignoreDuplicates.
Ignore duplicate values for primary keys? (not supported by MSSQL or Postgres < 9.5)
Besides, it's important to add a unique constraint for the message_uuid field on the model. So that the query will use ON CONFLICT DO NOTHING clause of Postgres.
For example, "sequelize": "^5.21.3" and postgres:9.6:
import { sequelize } from '../../db';
import { Model, DataTypes } from 'sequelize';
class Communication extends Model {}
Communication.init(
{
firstname: DataTypes.STRING,
lastname: DataTypes.STRING,
age: DataTypes.INTEGER,
message_uuid: {
type: DataTypes.INTEGER,
unique: true,
},
},
{ sequelize, tableName: 'communications' },
);
(async function test() {
try {
await sequelize.sync({ force: true });
// seed
await Communication.create({ firstname: 'teresa', lastname: 'teng', age: 32, message_uuid: 123 });
// test
await Communication.bulkCreate([{ firstname: 'teresa', lastname: 'teng', age: 32, message_uuid: 123 }], {
ignoreDuplicates: true,
});
} catch (error) {
console.log(error);
} finally {
await sequelize.close();
}
})();
Execution result:
Executing (default): DROP TABLE IF EXISTS "communications" CASCADE;
Executing (default): DROP TABLE IF EXISTS "communications" CASCADE;
Executing (default): CREATE TABLE IF NOT EXISTS "communications" ("id" SERIAL , "firstname" VARCHAR(255), "lastname" VARCHAR(255), "age" INTEGER, "message_uuid" INTEGER UNIQUE, PRIMARY KEY ("id"));
Executing (default): SELECT i.relname AS name, ix.indisprimary AS primary, ix.indisunique AS unique, ix.indkey AS indkey, array_agg(a.attnum) as column_indexes, array_agg(a.attname) AS column_names, pg_get_indexdef(ix.indexrelid) AS definition FROM pg_class t, pg_class i, pg_index ix, pg_attribute a WHERE t.oid = ix.indrelid AND i.oid = ix.indexrelid AND a.attrelid = t.oid AND t.relkind = 'r' and t.relname = 'communications' GROUP BY i.relname, ix.indexrelid, ix.indisprimary, ix.indisunique, ix.indkey ORDER BY i.relname;
Executing (default): INSERT INTO "communications" ("id","firstname","lastname","age","message_uuid") VALUES (DEFAULT,$1,$2,$3,$4) RETURNING *;
Executing (default): INSERT INTO "communications" ("id","firstname","lastname","age","message_uuid") VALUES (DEFAULT,'teresa','teng',32,123) ON CONFLICT DO NOTHING RETURNING *;
Check the database, there is only one row as expected.
node-sequelize-examples=# select * from communications;
id | firstname | lastname | age | message_uuid
----+-----------+----------+-----+--------------
1 | teresa | teng | 32 | 123
(1 row)
see the new UPSERT feature in Sequelize v6:
https://sequelize.org/api/v6/class/src/model.js~model#static-method-upsert
Implementation details:
MySQL - Implemented with ON DUPLICATE KEY UPDATE`
PostgreSQL - Implemented with ON CONFLICT DO UPDATE.
If update data contains PK field, then PK is selected as the default conflict
key. Otherwise first unique constraint/index will be selected, which
can satisfy conflict key requirements.
SQLite - Implemented with ON CONFLICT DO UPDATE
MSSQL - Implemented as a single query using MERGE and WHEN (NOT) MATCHED THEN
as second argument of Model.create you can provide onConflict prop, please read the documentation

Error: table data has 1 columns but 3 values were supplied Unable to execute statement

I'm testing this simple database program using a QML project:
Window {
visible: true
width: 640; height: 480
title: qsTr("SQL Example")
property var db
property int ident: 0
TextField {
id: field
placeholderText: qsTr("Enter Your Name")
hoverEnabled: true
}
Button {
text: "Next"
anchors.top: field.bottom
onClicked: storeData(field.displayText)
}
Component.onCompleted: initDatabase()
function initDatabase() {
db = LocalStorage.openDatabaseSync("data", "1.0", "Save names", 1000000)
db.transaction( function(tx)
{ tx.executeSql('CREATE TABLE IF NOT EXISTS data (id INTEGER, name TEXT, mode TEXT)') })
}
function storeData(username) {
db.transaction( function(tx) {
tx.executeSql('INSERT INTO data VALUES (?, ?, ?)', [ident, username, ""])
ident++ })
}
}
When I hit the Next button, after entering a name, I get this error message:
qrc:/main.qml:36: Error: table data has 1 columns but 3 values were supplied Unable to execute statement
Seemingly everything is right but I don't know why I get this error!
The query that you have.
INSERT INTO data VALUES (?, ?, ?)
The query that must be used as a better practice
INSERT INTO data (col1, col2, col3) VALUES (?, ?, ?)
Thing to note
Check if the table is already present in the database. That may be the reason why the create table statement is not executing with three column definition. If so remove the table and execute the code it will work.

#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 :(

I try to upload my backup SQL ( 10 days ago ) and get the following error:
Error
SQL query:
INSERT INTO `tlc_2_icl_strings` (`id`, `language`, `context`, `name`, `value`, `string_package_id`, `type`, `title`, `status`, `gettext_context`, `domain_name_context_md5`) VALUES
(339, 'en', 'us', 'ff242757e5c1e721795821ecbb4faa47', 'Menu Hover Text Color', NULL, 'LINE', NULL, 0, '', 'd17598730ad5b89d5156deab80901e2c'),
(340, 'en', 'us', 'cc5873ef6cc4407707d02137e62bca9c', 'Dropdown Background Color', NULL, 'LINE', NULL, 0, '', '6bc21a9c11719c10eb36ceaccde586d5'),
(341, 'en', 'us', '7438cef42306265c506b8c08ea81e169', 'Dropdown Text Color', NULL, 'LINE', NULL, 0, '', 'e3b512326d7eaf37193cc6d27acae76d'),
(342, 'en', 'us', 'bb38a8990bde7186a9f94d255f2c558b', 'Dropdown Hover Background Color', NULL, 'LINE', NULL, 0, '', '83dd52f7f96bcfb5677d842d86006ce7'),
(343, 'en', 'us', '5f71e49358bad1a47d34b1004ab9535f', 'Dropdown Hover Text Color', NULL, 'LINE', NULL, 0, '', '4de2db7c374f895b6d2e07b2e6490202'),
(344, 'en', 'us', '83a985eb08f189a5a39f866a9c8627a9', 'Dropdown Active Background Color',[...]
MySQL said: Documentation
#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 ''cc348e437dc74681e343' at line 91
I don't know what to do! Thank you in advance...

Sencha Touch SQL proxy

I am using SQL proxy in my Sencha Touch 2 app and I am able to store and retrieve data offline.
What I cannot do and what the Sencha documentation does not seem to provide is how do I customize the Sencha SQL store.
For eg, to create a SQL based store, I did the following -
Ext.define("MyApp.model.Customer", {
extend: "Ext.data.Model",
config: {
fields: [
{name: 'id', type: 'int'},
{name: 'name', type: 'string'},
{name: 'age', type: 'string'}
],
proxy: {
type: "sql",
database: "MyDb",
}
}
});
1. Now, how do i specify the size of the database ?
2. How do I specify constraints on the fields like unique, primary key etc ?
Say, I have 4 columns in my database :
pid, name, age, phone
I want to have a primary key for multiple fields : (pid, name)
If I was creating a table via SQL query, I would do something like -
CREATE TABLE Persons
(
pid int,
name varchar(255),
age int,
phone int,
primary key (pid,name)
);
Now how do I achieve the same via model ?
3. If I want to interact with the database via SQL query, I do the following -
var query = "SELECT * from CUSTOMER";
var db = openDatabase('MyDb', '1.0', 'MyDb', 2 * 1024 * 1024);
db.transaction(function (tx) {
tx.executeSql(query, [], function (tx, results) {
// do something here
}, null);
});
Is this the best way to do it?