Sequelize & mysql: clause `[tableName].deleted_at IS NULL` getting added automatically - mysql

using mysql + sequelize in a React/Node app;
a where the clause below gets automatically added which is screwing up my other where clause statements
[tableName].deleted_at IS NULL
in the nodeJS console, I can see the queries which all have this where clause...
tried recreating indexes, deleting this column throws an error that this field is not available;
column definition is:
`deleted_at` DATETIME NULL DEFAULT NULL,

Sequelize models provide you with a table settings option called paranoid.
In short, it's providing you with a deleted state for each row in that table, and that flag will be included in every query while using the ORM methods to work with the DB on this model.
So when you set the paranoid option to true, deletedAt column will be added as date and time and null as default.
MyModel.init({ /* attributes here */ }, {
sequelize,
paranoid: true,
// If you want to give a custom name to the deletedAt column
deletedAt: 'destroyTime'
});
If you actually check the raw queries, you will find the addition to your WHERE statement WHERE deletedAt IS NULL on all the queries.
You can find out more from the official Sequelize documentation here:
https://sequelize.org/master/manual/paranoid.html

Related

Grails: Db row update fails silently when domain class contains 'version false'

I'm using grails v3.2.9
I have a domain class Offer containing
static mapping = {
version false
}
I insert a row to a offer table, then in another transaction I try to update a value of one column inside that row, but offer update silently fails while other entities in the same transaction are updated properly.
I save the offer as follows:
offer.save(failOnError: true)
so it is not the case of offer.save() when the validation fails and saving fails silently.
However if I add version column to offer table(dbCreate is set to none) and change the Offer domain class to contain
static mapping = {
version true
}
the row starts to be updated successfully.
When I inspect the audit_log for offer table there is only the insertion events, no any update event is there.
It is very weird as I have other domain classes containing version = false and updating there works fine.
Any help would be appreciated.
Since version = false, the property Offer.version is equal to null and the column does not exist in database. Normally when you perform updates Hibernate will automatically check the version property against the version column in the database. So, I am just guessing it may be a bug with the hibernate session trying check a value that is null. I tried to replicate your scenario but I did not succeed.
Have you tried flushing the session when you save?:
offer.save(flush: true, failOnError: true)
It is also possible that when you changed the domain that required version to a domain that doesn't require version, the underlying database table didn't drop that column.
By default the version column is created as NOT NULL in the physical database. Even though hibernate doesn't care about the version property in the domain, the physical database won't let that record inserted and hence it fails.
While this explains why a record is not inserted, it doesn't explain why it doesn't throw an error. It shouldn't fail silently and instead throw a SQL exception.

Why would you set `null: false, default: ""` on a required DB column?

I'm building a Rails app with Devise for authentication, and its default DB migration sets the following columns:
## Database authenticatable
t.string :email, null: false, default: ""
t.string :encrypted_password, null: false, default: ""
What is the purpose of setting null: false and default: "" at the same time?
My understanding is that null: false effectively makes a value required: i.e., trying to save a record with a NULL value in that column will fail on the database level, without depending any validations on the model.
But then default: "" basically undoes that by simply converting NULL values to empty strings before saving.
I understand that for an optional column, you want to reject NULL values just to make sure that all data within that column is of the same type. However, in this case, email and password are decidedly not optional attributes on a user authentication model. I'm sure there are validations in the model to make sure you can't create a user with an empty email address, but why would you set default: "" here in the first place? Does it serve some benefit or prevent some edge case that I haven't considered?
Broadly speaking:
To make a column required, you must set null: false on it. This is true whether you are creating a new table or updating an existing one.
And in the event that you're updating an existing table, the DB engine will try to populate that new column with NULL in each row. In such cases, you must override this behavior with default: "", or else it will conflict with null: false and the migraiton will fail.
With respect to Devise:
Devise uses two separate templates for building migrations: migration.rb, for creating new tables, and migration_existing.rb, for updating existing tables (see source on GitHub). Both templates call the same migration_data method to generate the lines in question (i.e., the ones that specify null: false, default: ""), but as mentioned above, default: "" is only really relevant in the latter case (see O. Jones’ answer for more).
So the short answer to your question, specifically in the case of Devise migrations, is “because the generator uses borrowed code which doesn’t always apply, but still doesn’t break anything.”
A consideration for UNIQUE columns:
Note that in most popular SQL engines, uniquely indexed columns can still contain multiple rows of NULL values, as long as they are not required (naturally). But the effect of making a new column both required and unique (i.e., null: false, default: "", and unique: true) is that it cannot be added: the DB engine tries to populate the new column with an empty string in each row, which conflicts with the unique constraint and causes the migration to fail.
(The only scenario in which this mechanism fails is if you have exactly one row in your table — it gets a blank string value for the new column, which naturally passes the uniqueness constraint because it's the only record.)
So another way to look at it might be that these options are a safety mechanism preventing you from running migrations that you shouldn't (i.e., retroactively adding required columns to an already-populated table).
There is a difference in the insertion type. For example, let say you have a new_table table such that:
CREATE TABLE IF NOT EXISTS `new_table` (
`col1` VARCHAR(10) NOT NULL,
`col2` VARCHAR(10) NOT NULL DEFAULT '',
`col3` VARCHAR(10) NULL DEFAULT '');
When you use explicit insert of NULL you'll get the NULL:
INSERT INTO new_table(col1,col2,col3) VALUES('a','b',NULL);
'a','b',NULL
for col2 same trick will result in error:
INSERT INTO new_table(col1,col2,col3) VALUES('a',NULL,'c');
But when you use implicit insert of NULL you'll get the default value:
INSERT INTO new_table(col1,col2) VALUES('a','b');
'a','b',''
meaning that setting a default value is not preventing NULL assertion to this column, but only used when the value is not explicitly given.
Some application software gacks on NULL values but not on zero-length text strings. In Oracle, they're the same thing, but not in MySQL.
Things get interesting upon altering tables to add columns. In that case a default value is mandatory, so the DBMS can populate the new column.
I'm thinking this is here because of MySQL 'strict mode' not allowing you to disallow a null value without providing a default.
From mysql docs: https://dev.mysql.com/doc/refman/8.0/en/data-type-defaults.html
For data entry into a NOT NULL column that has no explicit DEFAULT clause, if an INSERT or REPLACE statement includes no value for the column, or an UPDATE statement sets the column to NULL, MySQL handles the column according to the SQL mode in effect at the time: If strict SQL mode is enabled, an error occurs for transactional tables and the statement is rolled back. For nontransactional tables, an error occurs, but if this happens for the second or subsequent row of a multiple-row statement, the preceding rows are inserted. If strict mode is not enabled, MySQL sets the column to the implicit default value for the column data type.

SQL NULL insertion not working

I'm having some odd SQL problems when inserting new rows into a table. I have set some columns to NULL, as I have with another table in my database. Obviously when no data is passed through on insertion it should enter NULL into the record, however currently it is not.
I have checked all settings in comparison with my other table (which is inserting records as NULL correctly) but can't find the issue. The columns appear as below, in both tables.
`statement_1` varchar(255) DEFAULT NULL,
No data is being pasted through (so not a blank space issue). Can anyone suggest why one table is doing as expected but the other is not?
Using below as the insert statement
$statement_a = "INSERT INTO statements (ucsid, statement_1, statement_2, statement_3, statement_4, statement_5, statement_6, statement_7, statement_8, statement_9, statement_10) VALUES (:ucsid, :statement_1, :statement_2, :statement_3, :statement_4, :statement_5, :statement_6, :statement_7, :statement_8, :statement_9, :statement_10)";
$q_a = $this->db_connection->prepare($statement_a);
$q_a->execute(array(':ucsid'=>$ucsid,
':statement_1'=>$statement_1,
':statement_2'=>$statement_2,
':statement_3'=>$statement_3,
':statement_4'=>$statement_4,
':statement_5'=>$statement_5,
':statement_6'=>$statement_6,
':statement_7'=>$statement_7,
':statement_8'=>$statement_8,
':statement_9'=>$statement_9,
':statement_10'=>$statement_10));
I can not add comments as I am new:
Try a simple INSERT statement using NOT phpmyadmin. Try
http://www.heidisql.com/ OR https://www.mysql.com/products/workbench/
INSERT INTO statements (ucsid) VALUES (123)
INSERT INTO statements (ucsid, statement_1) VALUES (123, NULL)
In both cases the statement_1 should be NULL. Which in your case most likely is not. However that would tell the problem lies in the database table and NOT with php or the php execute method you are using.
Also is the statement_1 field defined as NOT NULL and the default set as NULL? which can not happen.
Try recreating a new database and a new table with no records and than try inserting NULL as values as a test.
Also can you post the SQL of your database and table with Character Set and Collation
I've fixed the issue by ensuring that NULL is passed through the functions if nothing has been inserted by using the following code
if($_POST['statement_1'] == '') { $statement_1 = NULL; } else { $statement_1 = $_POST['statement_1']; }
Here the value passed by the varriable $statement_1 will be ""
Try this query SELECT * FROM my_table WHERE statement_1 ="".You will get rows.
Which means you are assigning some values to $statement_1 else it should be null.
Check your code. Hope this helps

Reset to MySQL DEFAULT value on update in CakePHP

I am wondering how to reset a field to the DEFAULT value (the one set in MySQL structure) when performing an update action in CakePHP. Like using the DEFAULT keyword in SQL:
INSERT INTO items (position) VALUES (DEFAULT);
edit: I am not searching for a way to use the default on create, I am rather looking for a way to reset the field to it's default when it has been already used.
You can simply unset the form input from the requested array, if you want to save its default value into the mysql database. You can try the following to achieve the same:
$item_details = $this->request->data;
unset($item_details['Item']['position']);
$this->Item->create();
$this->Item->save($item_details);
According to your edited question, if you want to reset any field during updating a record. you just need to use the MySql default() function.
$item_details = $this->request->data;
$this->Item->id = $item_details['Item']['id'];
$this->Item->saveField('position', DboSource::expression('DEFAULT(position)'));
To answer my own question, it could be done with:
$this->Item->saveField('position', DboSource::expression('DEFAULT(position)'));
or
$data['Item']['position'] = DboSource::expression('DEFAULT(position)');
$this->Item->save($data)
But - and here we go with the lost hours: to be able to use DboSource there had to be a database query before! Otherwise CakePHP throws the error Class 'DboSource' not found.

linq to sql string property from non-null column with default

I have a LINQ to SQL class "VoucherRecord" based on a simple table. One property "Note" is a string that represents an nvarchar(255) column, which is non-nullable and has a default value of empty string ('').
If I instantiate a VoucherRecord the initial value of the Note property is null. If I add it using a DataContext's InsertOnSubmit method, I get a SQL error message:
Cannot insert the value NULL into column 'Note', table 'foo.bar.tblVoucher'; column does not allow nulls. INSERT fails.
Why isn't the database default kicking in? What sort of query could bypass the default anyway? How do I view the generated sql for this action?
Thanks for your help!
If you omit the column, the value becomes the database default, but anything you insert is used instead of the default, example:
INSERT INTO MyTable (ID, VoucherRecord) Values(34, NULL) -- Null is used
INSERT INTO MyTable (ID) Values(34) -- Default is used
Picture for example you have a column that defaults to anything but NULL, but you specifically want NULL...for that to ever work, whatever value you specify MUST override the default, even in the case of NULL.
You need to set Auto-Sync to OnInsert, Auto Generated Value to true and Nullable to false for your column to work. See here for a full run-down with explanation on the Linq side.
For viewing the generated SQL, I have to recommend LinqPad