ID column error during insert - node.js

I am getting errors when trying to insert new records into my database (Postgres) using Node.js with Objection.js as the ORM/db handler. I have followed the tutorial, but I cannot find a mention of this in their current docs.
Here are my errors:
When debugging to console: error: null value in column "id" violates not-null constraint
When receiving the error via Postman, e.g. (for searchability with search engines): error: null value in column "id" violates not-null constraint
It looks like the 'id' column is being inserted to with a 'null' value from the logged query; it should not be inserting at all because it uses a default sequence value (auto-identity field, in other SQL solutions).
How do I insert new entities?

It turned out that I was defaulting the id to null on the TypeScript/Javascript model, and Objection.js expects the property to not be defined.
So do one of the following:
Don't create the property (property, and thus value, will be undefined)
Create the property, but don't set a value (value will be undefined)
Set the value to undefined
delete the property
The reason it was setting it to null was because other ORMs I was trying supported setting it to null. Typeorm, in particular, allows marking a column as Primary and a Generated column (meaning it doesn't try to insert the value; the database's table definition is responsible for creating the value). Also, other non-JS ORMs also support either null or an invalid id value, such as zero, to indicate that the object does not yet exist in the table, and therefore should be inserted (again, with ways to indicate that the column is both primary and generated).

Related

Is Sequelize Typescript column data type not strict?

I am using an SQLite database with Sequelize. On top of Sequelize, I am using Sequelize-Typescript. I have defined a table and gave every column a specific DataType. I assumed that it would then also validate that upon adding rows or updating rows, it would check whether the data indeed is of that DataType. However, after testing, I could add an instance with a boolean in a column with the DataType STRING(128).
Is this normal behavior? If so, what is the correct way to validate input datatypes?

Multiple rows insert operation in Postgres should return failed rows information

I want to use insert multiple rows using a single insert statement in Postgres.
Here, the catch is that if insert fails for single row, all other successful inserts are being rollbacked. Is there a way to avoid the rollback and make the query return list of failed rows.
Else, I end up in writing a loop of insert statements. I am using Node pg module. Is there a recommended way of achieving my requirement if postgres doesn't support this.
Edit - 1
insert into test(name, email) values ('abcd', 'abcd#a'), ('efgh', 'blah'), (null, 'abcd') ON CONFLICT DO NOTHING;
ERROR: null value in column "name" violates not-null constraint
DETAIL: Failing row contains (9, null, abcd).
After above query, select statement returns 0 rows. I am looking for a solution wherein, the first two rows get inserted.
Sounds like the failure you're talking about is hitting some sort of unique constraint? Take a look at PostgreSQL INSERT ON CONFLICT UPDATE (upsert) use all excluded values for a question related to insert... on conflict usage.
For example if you do INSERT INTO X VALUES (...100 rows...) ON CONFLICT DO NOTHING; Any duplicates that collide with the primary key will just be ignored. The alternate to DO NOTHING is to do an UPDATE on conflict.
EDIT to match newly stated question. On conflict does not help with null constraint violations. You can do a WITH clause and select only the values without null properties. Here's a sample I just tested in Postgres:
create extension if not exists pgcrypto;
create table tmp (id uuid primary key default gen_random_uuid());
with data_set_to_insert as (
select x.id from (values (null), (gen_random_uuid())) x(id) -- alias x can be anything, does not matter what it is
)
insert into tmp(id) select id from data_set_to_insert where id is not null returning *;

Acumatica Formula for Dividing 2 values

I am trying to divide values and display it in a new custom field(usrQuantity)on Stock Items Screen.
I want to divide OpenQty (which is a column in POLine) and CARTONQTY(which is not a column name but just an attribute in column AttributeID in CSAnswers table).
enter image description here
I am confused how to perform this division since CARTONQTY is not a field, I noticed that there is a field named CARTONQTY_Attributes in InventoryItem table which has been generated by some Join queries but is not actually present in the Database(checked in SQL Management Studio).
enter image description here
I tried this formula in the DAC of usrQuantity
[PXDBInt]
[PXUIField(DisplayName="Quantity")]
[PXFormula(typeof(Div<POLine.orderQty,InventoryItem.CARTONQTY_Attributes>))]
But it is giving following errors
The type or namespace name 'POLine' could not be found (are you missing a using directive or an assembly reference?)
The type name 'CARTONQTY_Attributes' does not exist in the type 'PX.Objects.IN.InventoryItem'
Do you want to store the value in the db? Typically it's not recommended practice to persist calculated values unless, for example, there's a performance issue in performing the calculation on the fly. If you don't want to store it, you probably want a PXInt instead of PXDBInt. Also, unless you always expect a whole number as a result of your division (which is unlikely), you should probably use a PXDecimal type.
To then get your calculated value into your new field, I would probably set it in RowSelecting and RowUpdated event handlers by extending the appropriate PXGraph class; in order to calculate it as you retrieve a row and when you update the row values.

Duplicate Error on adding data in mongoDB

I am trying to insert data in mongoDB document but I am getting duplicate error in it.
While defining the schema, I have given the value of "role" as default:"customer".
But on adding data with different number, i am getting this error.
Following is the schema defined:
Where is the error?
Check if the collection have unique index on the column.
db.customers.getIndexes()
here you will get unique index on userName. drop it
It will return all the indexes. If it have unique: true then drop it and then insert records.
OR just use
db.customers.dropIndex('username_1')
It's not the error because of mobilenumber. it's because of username. you are trying to insert multiple username with null value which is violating unique rule.

CRecordset::IsFieldNull method replacement in bulk row fetching

I have query related to bulk row fetching using CRecordSet (MFC ODBC).
On the MSDN page, it is written that
The member functions IsDeleted, IsFieldDirty, IsFieldNull, IsFieldNullable, SetFieldDirty, and SetFieldNull cannot be used on recordsets that implement bulk row fetching. However, you can call GetRowStatus in place of IsDeleted, and GetODBCFieldInfo in place of IsFieldNullable.
Now, I want to check whether a field contains "NULL"/"has no value" data. How can I check this as the IsFieldNull function does not work in bulk row fetching?
There is the difference between IsFieldNull and IsFieldNullable function.
So logically you will not be able to know whether a filed is null for a particular row since you are doing bulk row fetching. But you can only determine whether a particular field is nullable which simply means if that field is capable of accepting null values.
The CODBCFieldInfo structure contains information about the fields in an ODBC data source.
It has a member called m_nNullability which identifies Whether the field accepts a Null value. This can be one of two values: SQL_NULLABLE if the field accepts Null values, or SQL_NO_NULLS if the field does not accept Null values.
So pass the object of CODBCFieldInfo structure to CRecordset::GetODBCFieldInfo function which collects the object by reference. So no need to worry, you will get the updated value back and then check the member m_nNullability value of that object to only know whether the filed is nullable and not whether a field for a particular row is null.
http://msdn.microsoft.com/en-us/library/xexc6xef(v=vs.80).aspx
http://msdn.microsoft.com/en-us/library/k50dcc9s(v=vs.80).aspx
CRecordset::GetODBCFieldInfo function has two versions. One version of the function lets you look up a field by name. The other version lets you look up a field by index.

Resources