Im using Laravel framework and it gives an error with the DB
Error message:
[2016-04-25 06:07:34] local.ERROR: exception 'PDOException' with
message 'SQLSTATE[HY000]: General error: 1364 Field 'remarks' doesn't
have a default value' in ...
The field 'remarks' has a default value of 'None' set in PHPMyAdmin. I dont understand why does it gives an error when it has a default value set. I believe that 'None' is a string value so it's not like a NULL value.
$aId = DB::table('attachments')->insertGetId([ 'document_type_code'=>$document_id, 'report_no'=>'report '.$document_id,
'file_attachment_link'=>$filepath, 'file_attachment_upload'=>$file->getClientOriginalName(), 'uploaded_at'=> $now, 'uploaded_by' => 1,
//Auth::user()->id 'version_number' => 1, ]);
None is not a default string value. It means that there is no default value set.
You can either pass a value in your INSERT statement or alter the table to actually hold a default value for the column.
You can use this sql statement to alter the table
ALTER TABLE attachments MODIFY COLUMN `remarks` VARCHAR(255) DEFAULT 'something';
Or do it from PhpMyAdmin
Don't edit the tables directly. You have your model for that.
Generate a new migration and set you field to be nullable:
$table->string('name', 50)->nullable();
and then php artisan migrate
Related
Here's my migration schema:
public function up()
{
Schema::create('objects', function (Blueprint $table) {
$table->increments('id');
$table->timestamp('timestamp1');
$table->timestamp('timestamp2');
});
}
But when I execute php artisan migrate, I get this error:
Illuminate\Database\QueryException : SQLSTATE[42000]: Syntax error or access violation: 1067 Invalid default value for 'timestamp2' (SQL: create table objects (id int unsigned not null auto_increment primary key, timestamp1 timestamp not null, timestamp2 timestamp not null) default character set utf8mb4 collate utf8mb4_unicode_ci)
I must indicate that when I remove one of the 2 $table->timestamp(...); lines it works, but it doesn't when there is both. And the Object.php model is empty as it can be. Did I make a mistake?
I have read this post, but even though there is no longer errors when I change timestamp(...) into dateTime(...), I only want timestamps.
Timestamps are a little special, they must either be nullable or they must have a default value. So you must choose between timestamp('timestamp1')->nullable(); or timestamp('timestamp1')->useCurrent() or a custom default value like timestamp('timestamp1')->default(DB::raw('2018-01-01 15:23')).
I found this solution on laracasts:
nullableTimestamps() are only for default fields created_at, updated_at. for custom fields use timestamp()->nullable();
You can make one of the two timestamps nullable by using
timestamp()->nullable();
using your example, you would use:
$table->timestamp('timestamp2')->nullable();
Also laravel has built in timestamps by using
$table->timestamps();
which would automatically handle updated_at and created_at timestamping for you
The error is:-
Field 'attn' doesn't have a default value41
If I set all the value of column query run perfectly like :
$stuattend = "INSERT INTO tblattendance(roll, attandence, att_time) VALUES('$roll', 'present', '5:30')";
I guess my problem is when I execute one value (roll) through query.
What would be the solution?
Hi the error clearly states that you have a column named as attn which is not nullable field by default. Either provide the default value or make it nullable or pass it some value so that it stops throwing the error.
You can change the above query to assign the default value as follows :
NOTE : Here you may assign the any value to it. Thinking that it may be integer I am doing as follows
INSERT INTO tblattendance(roll, attandence, att_time, attn) VALUES('$roll', 'present', '5:30', 10)
i used to work with laravel 5.2 now i've upgraded to 5.4
i usually use my validation to pupolate object befor sending it to database like
$validation_rules = ['title'=>'required' , 'number'=>'present|int'];
// do validation
$obj = new Object();
foreach($validation_rules as $k=>$v )
$obj->$k = $request->input($k);
now if the user doesnt send the number parameter it would be null or false in the nnumer property of my object ... in the older versions it would automaticly change to default value of that column ... for example if i have number column type as int when inserting this object the number column would be 0
but now this doesnt happen instead im getting this error
Integrity constraint violation: 1048 Column 'number' cannot be null
btw strict mode is off
pleas note i know all about nullable , i dont want to make those fields nullable thats the whole point... its about automatically converting null values to the default column type value when field is not nullable
In Laravel 5.4 the concept of ->default(0) is different than from Laravel 5.2. In 5.4, the default only works if you don't pass anything, so when you do
$obj->$k = $request->input($k);
if $request->input($k) is null, you are sort of 'inserting' null into a column that is not nullable, which causes the error.
So it seems you have to make the column nullable in the DB and do a check in your controller:
$obj->$k = $request->input($k) ? $request->input($k) : 0;
Make sure your column 'number' is nullable. If you are using Laravel migrations to create your table, you will need to add it as $table->integer('number')->nullable();
I am migrating a database from MySQL to MSSQL.
[MySQL] I have a CHANGEDATE column that is of TIMESTAMP with default CURRENT_TIMESTAMP
[MSSQL] I have the same CHANGEDATE column that is of DATETIME and added a default constraint of GETDATE()
The codebase is PHP using CodeIgniter. I want the column to always be set so I don't allow NULL in either DBMS.
When I insert with MySQL, the property of the PHP model CHANGEDATE defaults to NULL. This triggers the default and the column entry is set to CURRENT_TIMESTAMP. The same code when configured to MSSQL however throws an error that NULL is not allowed in the column, which is valid, but I would rather MSSQL function like MySQL and insert the value of GETDATE() in that instance.
If I do unset($model->CHANGEDATE) or delete the property from my model, then it works as expected, but I wanted to know if there was a way to solve this just using MSSQL instead of updating all my PHP models.
class model {
public $CHANGEDATE;
...
}
ERROR (as described):
[Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Cannot insert the value NULL into column 'CHANGEDATE'; column does not allow nulls. INSERT fails.
INSERT INTO Logs (..., CHANGEDATE, CHANGEBY) VALUES (..., NULL, NULL)
UPDATE:
CI should create support for DBMS specific keywords as #steoleary stated in his answer(for which I marked his correct). However, I found the best solution in my case was to slightly modify the core class DB_active_rec.php
function set(...){
...
foreach ($key as $k => $v)
{
if (is_null($v)) continue;
...
}
}
I assume that you already have the default set on your SQL server column and you don't allow NULLs, deafult constraints won't fire on a NULL value, they will only fire when no value is specified, or if you specify to insert the default value on insert like this:
INSERT INTO [dbo].[table]
([col1]
,[col2]
,[col3]
,[col4]) --Column with default constraint
VALUES
('bob',
'bobson',
1,
DEFAULT) --default keyword
Doing that will cause the default to fire and you shouldn't have to change your models.
I don't know how to express this in code igniter, but in SQL Server, it is really easy:
create table . . . (
changedate not null datetime default getdate()
)
No trigger is required.
I've setup a post validator in my symfony form to stop duplication of primary keys.
A primary key is a two-character string in this instance. Code used to validate:
$this->mergePostValidator(new sfValidatorDoctrineUnique(array(
'model' => 'Manufacturers',
'column' => 'id',
'primary_key' => 'id'
)));
The primary key is uppercase (for example AU). Bizarrely the post validator triggers successfully is lowercase 'au' is entered into the field (i.e. stops it from going to the database and triggering a 500 integrity constraint error), but if entered correctly as 'AU' it doesn't seem to notice the duplication.
Any thoughts?
That's not a symfony sfDoctrineValidator issue. All this validor does is to search your database for an existing record. If you are using a "_ci" (case-insensitive) collation (are you using mysql?) the search returns nothing - the validator is fooled.
Then when you insert the duplicate, you get a exception from the database. Try to change the collation of your table like this:
ALTER TABLE `table` DEFAULT CHARACTER SET utf8 COLLATE utf8_bin
(you should tell doctrine to do it for you:
MyTable:
options: { collate: utf8_bin, charset: utf8 }
)