can you post your model?
can you post your model?
According to the docs, it should be auto generated but I see it in the query which make me think that you have it defined in the model. If I understand well, it should not be in your model rules at all!
The issue originally occurred when I had not yet added anything regarding this new column. I have since tried all sorts of variations (model, controller, …), nothing I have tried has worked and I can’t find anything in the documentation.
/**
* This is the model class for table "employees".
*
* @property int $EmpId
* @property int $EmpNo
* @property string $FirstName
* @property string $LastName
* @property string $Company
* @property int $TitleId
* @property string $Address1
* @property string $Address2
* @property int $CityId
* @property int $ProvId
* @property int $CountryId
* @property string $PostalCode
* @property string $BusinessTel
* @property string $BusinessTelExt
* @property string $HomeTel
* @property string $CellularTel
* @property string $Fax
* @property string $Email
* @property string $URL
* @property string $Notes
* @property string $SpecialNotes
* @property string $DL
* @property string $Ins
* @property string $CurrencyCode
* @property int $TaxRateId
* @property string $ContractorAgreement
* @property string $ContractorAgreementDt
* @property int $Active
* @property string $XeroContactId
* @property string $dtCreation
* @property string $dtModification
* @property int $OldEmployeeId
*
* @property EmployeesCert[] $employeesCerts
* @property EmployeesCertifications[] $employeesCertifications
* @property ProjectsLegs[] $projectsLegs
* @property EmployeesServices[] $employeesServices
*/
class Employees extends \yii\db\ActiveRecord
{
// public function beforeSave($insert)
// {
// if (!parent::beforeSave($insert)) {
// return false;
// }
// if ($this->isNewRecord) {
// // $this->Active = 1;
// // $this->OldEmployeeId = ''; // null;
// $this->RV = new Expression('DEFAULT'); // null;
// // $this->dtCreation = date("Y-m-d H:i:s");
// // $this->dtModification = date("Y-m-d H:i:s");
// } else {
// // $this->dtModification = date("Y-m-d H:i:s");
// }
// return true;
// // return parent::beforeSave($insert);
// }
/**
* @inheritdoc
*/
public static function tableName()
{
return 'employees';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['FirstName', 'CurrencyCode', 'TaxRateId'], 'required'],
[['EmpNo', 'TitleId', 'CityId', 'ProvId', 'CountryId', 'TaxRateId', 'OldEmployeeId'], 'integer'],
[['DL', 'Ins', 'dtCreation', 'dtModification', 'ContractorAgreementDt', 'OldEmployeeId'], 'safe'],
[['FirstName', 'LastName', 'Company', 'Address1', 'Address2', 'PostalCode', 'Email', 'URL', 'Notes', 'SpecialNotes'], 'string', 'max' => 255],
[['BusinessTel', 'BusinessTelExt', 'HomeTel', 'CellularTel', 'Fax'], 'string', 'max' => 50],
[['XeroContactId'], 'string', 'max' => 36],
[['CurrencyCode'], 'string', 'max' => 3],
[['Active', 'ContractorAgreement'], 'string', 'max' => 1],
[['FirstName', 'LastName', 'Company', 'Address1', 'Address2', 'PostalCode', 'BusinessTelExt', 'Notes', 'SpecialNotes', 'OldEmployeeId'], 'filter', 'filter' => 'trim'],
[['LastName', 'Company', 'Address1', 'Address2', 'PostalCode', 'Email', 'URL', 'Notes', 'SpecialNotes', 'BusinessTel', 'BusinessTelExt', 'HomeTel', 'CellularTel', 'Fax', 'OldEmployeeId', 'dtModification'], 'default'], //Ensure blanks are saved as Nulls!
[['CurrencyCode'], 'exist', 'skipOnError' => true, 'targetClass' => LstCurrencies::className(), 'targetAttribute' => ['CurrencyCode' => 'CurrencyCode']],
[['TaxRateId'], 'exist', 'skipOnError' => true, 'targetClass' => LstTaxRates::className(), 'targetAttribute' => ['TaxRateId' => 'TaxRateId']],
];
}
What I don’t get is when I review the error message, the columns don’t match the value sequence. RV is the last column yet the DEFAULT is list third to last?! I never thought adding a column would break my application like this, but RowVersion is a necessity for MSSQL, so I don’t know what to do at this point.
What is also odd is the fact that updates work just fine. It is only the Create action that fails like this even though both use the same function to perform the save.
protected function saveEmployee($model, $modelsDrivers, $modelsDriversServices, $modelsDriversCertifications)
{
$transaction = Yii::$app->db->beginTransaction();
try {
if ($go = $model->save(false)) { //****This is the line the error is being flagged on****
// loop through each Driver
foreach ($modelsDrivers as $i => $modelsDriver) {
as you can see validation shouldn’t be an issue as I am using save(false).
I found https://github.com/yiisoft/yii2/issues/19074 and so switched the column to allow NULLs, but it made no difference, still errs.
Customized save() for the create action?
(insert followed by an update)
Edit: The quoted text has an error.
Correct error mesage is:
SQLSTATE[HY000]: General error: 20018 Cannot insert an explicit value into a timestamp column. Use INSERT with a column list to exclude the timestamp column, or insert a DEFAULT into the timestamp column
So it is with a column list.
Add attribute list to save()?
https://www.yiiframework.com/doc/api/2.0/yii-db-baseactiverecord#save()-detail
How? How can I exclude the column?
See my edit above.
This is interesting:
Each database has a counter that is incremented for each insert or update operation that is performed on a table that contains a rowversion column within the database. This counter is the database rowversion. This tracks a relative time within a database, not an actual time that can be associated with a clock. A table can have only one rowversion column. Every time that a row with a rowversion column is modified or inserted, the incremented database rowversion value is inserted in the rowversion column.
https://dba.stackexchange.com/questions/210820/how-are-sqlserver-timestamp-rowversion-values-created
Just pass list of attributes you want to save. See: BaseActiveRecord, yii\db\BaseActiveRecord | API Documentation for Yii 2.0 | Yii PHP Framework
This is a strange thing indeed!
What happens if you run insert manually on SSMS?
Add attribute list to save()?
BaseActiveRecord, yii\db\BaseActiveRecord | API Documentation for Yii 2.0 | Yii PHP Framework
That seems very promising. I will try it, won’t be until much later in the day though, and will report back.
Thank you for pointing me to the save documentation and helping me.
Thank you for pointing me to this. I will try it later today, but it look very promising. I’ll report back.
I must be doing something wrong.
$model->save(false, [
'EmpId', 'EmpNo', 'FirstName', 'LastName', 'Company', 'TitleId', 'Address1', 'Address2', 'CountryId', 'ProvId', 'CityId', 'PostalCode',
'BusinessTel', 'BusinessTelExt', 'HomeTel', 'CellularTel', 'Fax', 'Email', 'URL', 'Notes', 'SpecialNotes', 'CurrencyCode', 'TaxRateId', 'ContractorAgreement',
'ContractorAgreementDt', 'Active', 'XeroContactId', 'dtCreation', 'dtModification', 'OldEmployeeId'
])
and yet, with no RV, I get the exact same error with RV specified?
I have an Idea that might work via a db trigger, far from ideal. I’ll play with that and post back.
What do you get as stack trace right now?
Same as before, no change.
Seems to be some confusion whether the db column can have DEFAULT specified (or not).
I created an Insert Trigger to take any RV value and omit it at Insert, it works, I tested with SSMS, but Yii is still blocking the action, isn’t even getting to the db.
I’m thinking I’ll have to resort to manually writing the SQL query to get this to work.
You can’t create a timestamp (includes RowVersion) column with DEFAULT, Azure won’t allow it and generates an error indicating that.
I suggest you open an issue for someone more knowledgeable to help.
How do I go about that?