Hi Vinicius,

I think there are two issues.

The fact that models are executed at every http request and therefore
values are set there is explained in the manual.
I can understand the confusion when coming form other frameworks but
there is nothing new there.

About the update. I agree there is a discrepancy between
implementation and documentation.
This sentence for example "The default value is used when performing
an insert if a value
 is not explicitly specified" does not make sense and should be "The
default value is used when performing an insert if a value
is explicitly specified."

The current design is motivated by the fact that it is not a good idea
to leave those values NULL (None) because they are supposed to be
dates. If you have None in there, you end up needing a lots of check
in the code when you try to format the dates to take care of None
dates.

update='...' is used for timestamps therefore its value should not be
displayed in the form. If it were to be displayed it would be wrong
anyway, because the stored value would be recomputed with the second
http request that performs the insert.

The current design still allows you to check whether a field was or
not updated by comparing the created_on with the updated_on date.

Because this is not clear in the docs we either have to revise the
docs or change its behavior (the latter would break some users'code)
and I think fixing the docs is a better approach.

Anyway, I'd like to hear more opinions about this.

Massimo



On Nov 8, 1:35 pm, Vinicius Assef <[email protected]> wrote:
> OK. Understood.
>
> But I must say it doesn't conform with web2py documentation.
>
> About default, the book says: default sets the default value for the
> field. The default value is used when performing an insert if a value
> is not explicitly specified. It is also used to pre-populate forms
> built from the table using SQLFORM.
>
> Assuming default==update may be a source of confusion and unexpected
> behaviour. I tried it today. I lost almost a day trying to figure it
> out.
> In spite of that, how can I have my example field filled with None
> when inserted and just populated when updated?
> Note that I'm running from shell because I intent to run this process
> by a cron job. So, http requests are not present here.
>
> About update, the book says: update contains the default value for
> this field when the record is updated.
>
> Well, again, assuming default==update is not what manual says.
> When developing database applications, update is understood as an
> event that occurs when something changes in your *existing* record (or
> row, if purists read this.). So, assume default content is equal
> update content may not be a good practice here, and, again, source of
> confusion.
>
> Another comment, if allowed, is about datetime.now() be evaluated just
> when the table is defined. It is not explicit in book, too.
> If I have a background process listening to some semaphores and
> updating and inserting records through a cron job, it's worthless and,
> again, my be a source of misunderstanding.
>
> Massimo, I ask you to think about that and inform us if web2py way of
> doing theses things will change or documentation will be updated to
> reflect this behaviour.
>
> --
> Vinicius Assef.
>
> On Mon, Nov 8, 2010 at 5:09 PM, mdipierro <[email protected]> wrote:
> > no. if you have update but not default, default==update.
>
> > On Nov 8, 1:02 pm, Vinicius Assef <[email protected]> wrote:
> >> Thanks, Massimo.
>
> >> But, when the record is inserted, the field with "update" param
> >> shouldn't be None?
>
> >> --
> >> Vinicius Assef.
>
> >> On Mon, Nov 8, 2010 at 4:58 PM, mdipierro <[email protected]> wrote:
> >> > This is misunderstanding. Yes what you have is correct.
>
> >> > The default=datetime.now() and update=datetime.now() is evaluated only
> >> > ONCE when the table is defined therefore when you insert and update
> >> > the values are always the same.
>
> >> > BUT
>
> >> > when the code is in a web2py controller, the model is run again at
> >> > every http request and therefore the values of default and request
> >> > will be updated.
>
> >> > Massimo
>
> >> > On Nov 8, 12:51 pm, Vinicius Assef <[email protected]> wrote:
> >> >> Buddies, this is what is happening, step by step:
>
> >> >>http://pastebin.com/b14HLy39
>
> >> >> This test was made with latest stable version: Version 1.88.2
> >> >> (2010-10-29 23:04:43)
>
> >> >> Again, am I missing something?
>
> >> >> I don't think there is a bug here. I expect I am making a really big
> >> >> and stupid mistake.
> >> >> This is too important to not be working out.
>
> >> >> By the way: I'm using Ubuntu 9.04.
>
> >> >> --
> >> >> Vinicius Assef.
>
> >> >> On Mon, Nov 8, 2010 at 3:13 PM, Vinicius Assef <[email protected]> 
> >> >> wrote:
> >> >> > Massimo, I expect the correct way is:
>
> >> >> > a) When inserting (sql insert) a new record:
> >> >> > 1) was_inserted_on receives request.now contents (this is ok)
> >> >> > 2) was_updated_on receives null
>
> >> >> > b) When updating (sql update) a record:
> >> >> > 1) was_inserted_on doesn't change is value (this is ok)
> >> >> > 2) was_update_on receives request.now
>
> >> >> > What is happening here, with web2py 1.87.3 and SQLite:
>
> >> >> > a) When inserting (sql insert) a new record:
> >> >> > 1) was_inserted_on receives request.now content
> >> >> > 2) was_updated_on receives request.now content
>
> >> >> > b) When updating (sql update) a record:
> >> >> > 1) was_inserted_on doesn't change its value
> >> >> > 2) was_update_on doesn't change its value
>
> >> >> > Am I loosing something?
>
> >> >> > --
> >> >> > Vinicius Assef.
>
> >> >> > On Mon, Nov 8, 2010 at 2:27 PM, mdipierro <[email protected]> 
> >> >> > wrote:
> >> >> >> What you expect is correct and I cannot reproduce the problem. how do
> >> >> >> you update the record?
>
> >> >> >> On Nov 8, 8:54 am, Vinicius Assef <[email protected]> wrote:
> >> >> >>> I have this model just for test:http://pastebin.com/vF4VBLLM
>
> >> >> >>> Field 'was_inserted_on' is working all right. It has default insert
> >> >> >>> value and it isn't updated across record updates. It's ok.
>
> >> >> >>> But 'was_updated_on' is working the same way.
>
> >> >> >>> I imagined it was null when record is inserted and in every record
> >> >> >>> update, it would be this column updated.
>
> >> >> >>> At least, it was what I got from we2bpy manual in Record
> >> >> >>> Representation section (DAL chapter).
>
> >> >> >>> Where did I misunderstood?
>
> >> >> >>> By the way, I solved that using compute in was_updated_on field and
> >> >> >>> returning request.now.
>
> >> >> >>> --
> >> >> >>> Vinicius Assef.
>
>

Reply via email to