Welcome, Guest. Please login or register.
Did you miss your activation email?
Sep. 02, 2010, 07:42:57 PM
53844 Posts in 11998 Topics by 11375 Members
Latest Member: elizablackmore7
News:
 
The PRADO Community » Prado v3.x » Bug Reports » [mysql] ActiveRecord demands setting of a column which has a default value « previous next »
Pages: [1] Print
Author Topic: [mysql] ActiveRecord demands setting of a column which has a default value  (Read 4237 times)
acron
Senior Member
***

Karma: 8
Offline Offline

Posts: 76



View Profile
« on: Jul. 24, 2008, 12:36:17 PM »

An ActiveRecord-Object refuses to save instead an exception is thrown if a member which corrosponds with a column  that has a default value in the mysql db is not set.

I've found the code that causes the error in TActiveRecordGateway.php:
Code:
if(!$column->getAllowNull() && $value===null && !$column->hasSequence())
{
    throw new TActiveRecordException(
      'ar_value_must_not_be_null', get_class($record),
      $tableInfo->getTableFullName(), $name);
}

The logic behind this code seems incorrect to me as it does not take a default value into account. I've patched it the following way
Code:
297c297,298
<                       if(!$column->getAllowNull() && $value===null && !$column->hasSequence())
---
>                       if(!$column->getAllowNull() && $value===null && !$column->hasSequence()
>                               && !$column->getDefaultValue())

For an example of the bug please see my previous thread:
http://www.pradosoft.com/forum/index.php/topic,10599.0.html

Please consider to apply the patch to svn if you don't see any mistakes.

Greetings acron

System:
Webserver: Apache2 with PHP Version 5.2.4-2ubuntu5.2
Prado: prado-3.1.2.r2448
DB: 5.0.51a-3ubuntu5.1
Logged

“A little inaccuracy sometimes saves tons of explanation.” H. H. Munro
rojaro
Administrator
Platinum Member
*****

Karma: 38
Offline Offline

Posts: 684


PRADO aint no voodoo ...


View Profile WWW
« Reply #1 on: Jul. 24, 2008, 06:48:34 PM »

Nice find/fix ... i second your request.
Karma++

Greetings from Hamburg / Germany
- rojaro -
« Last Edit: Jul. 24, 2008, 06:52:09 PM by rojaro » Logged

A mathematician is a machine for turning coffee into theorems. ~ Alfred Renyi (*1921 - †1970)
tof06
PRADO v3.x Developer
Platinum Member
*****

Karma: 87
Offline Offline

Posts: 1065



View Profile
« Reply #2 on: Jul. 25, 2008, 08:07:48 AM »

Your patch looks good.
Please, can you post a ticket (if not already done) referring this thread, we'll merge that in 3.1.3

Thanks.
Logged
rojaro
Administrator
Platinum Member
*****

Karma: 38
Offline Offline

Posts: 684


PRADO aint no voodoo ...


View Profile WWW
« Reply #3 on: Jul. 25, 2008, 08:24:30 AM »

I've just created ticket #887.

Greetings from Hamburg / Germany
- rojaro -
Logged

A mathematician is a machine for turning coffee into theorems. ~ Alfred Renyi (*1921 - †1970)
acron
Senior Member
***

Karma: 8
Offline Offline

Posts: 76



View Profile
« Reply #4 on: Jul. 25, 2008, 10:29:29 AM »

Thanks for your positive feedback.  Smiley
Just out of curiosity: Does anyone know if this is bound to mysql? Is a default value not marked "NOT NULL" in other DBMS?

Greets acron
Logged

“A little inaccuracy sometimes saves tons of explanation.” H. H. Munro
rojaro
Administrator
Platinum Member
*****

Karma: 38
Offline Offline

Posts: 684


PRADO aint no voodoo ...


View Profile WWW
« Reply #5 on: Jul. 25, 2008, 11:16:29 AM »

Does anyone know if this is bound to mysql? Is a default value not marked "NOT NULL" in other DBMS?

This is a feature specified in the ANSI SQL-92 standard, therefore all databases that implement this standard have support for this.
Also, all databases supported by PRADOs ActiveRecord implementation are (AFAIK) ANSI SQL-92 conform.

Greetings from Hamburg / Germany
- rojaro -
Logged

A mathematician is a machine for turning coffee into theorems. ~ Alfred Renyi (*1921 - †1970)
acron
Senior Member
***

Karma: 8
Offline Offline

Posts: 76



View Profile
« Reply #6 on: Oct. 30, 2008, 04:28:33 PM »

Code:
297c297,298
<                       if(!$column->getAllowNull() && $value===null && !$column->hasSequence())
---
>                       if(!$column->getAllowNull() && $value===null && !$column->hasSequence()
>                               && !$column->getDefaultValue())
Sorry, the fix is also buggy. As TDbTableColumn::getDefaultValue() returns TDbTableColumn::UNDEFINED_VALUE (which is defined as INF) the last condition always evaluates to true.
The correct condition would be:
($column->getDefaultValue()===TDbTableColumn::UNDEFINED_VALUE)
So Patch must be:
Code:
297c297,298
<                       if(!$column->getAllowNull() && $value===null && !$column->hasSequence())
---
>                       if(!$column->getAllowNull() && $value===null && !$column->hasSequence()
>                               && ($column->getDefaultValue()===TDbTableColumn::UNDEFINED_VALUE))

Note that there is no ! in front of the last condition.
I hope that this patch turns out better than the one before.

Greetings acron
Logged

“A little inaccuracy sometimes saves tons of explanation.” H. H. Munro
icolo
Senior Member
***

Karma: 5
Offline Offline

Posts: 77


View Profile
« Reply #7 on: Jun. 08, 2009, 10:48:44 AM »

In the method TActiveRecordGateway::getUpdateValues() there is a similar thing that I believe should be fixed like this:

Code:
if(!$column->getAllowNull() && $value===null && ($column->getDefaultValue()===TDbTableColumn::UNDEFINED_VALUE))
{
throw new TActiveRecordException(
'ar_value_must_not_be_null', get_class($record),
$tableInfo->getTableFullName(), $name);
}

Besides: has acron's fixed fix been applied to the code in the svn? I still found the old fix there, and I couldn't find an issue at code.google.com.
Logged
rojaro
Administrator
Platinum Member
*****

Karma: 38
Offline Offline

Posts: 684


PRADO aint no voodoo ...


View Profile WWW
« Reply #8 on: Nov. 01, 2009, 09:17:12 PM »

Merged in r2725. I assume the ticket got lost with the move from trac to google code.

Greetings from Rostock / Germany
- rojaro -
Logged

A mathematician is a machine for turning coffee into theorems. ~ Alfred Renyi (*1921 - †1970)
Pages: [1] Print 
« previous next »
Jump to: