Commit Graph

160 Commits

Author SHA1 Message Date
Raphael Collet 78a20a3dba [FIX] model: wrong alias used in read query for multi-inherited field
In commit 04ba0e99, we introduced an optimization for reading inherited fields
in a single query.  There is an issue when you have more than one level of
`_inherits`.  The query looks like:

    SELECT ...
    FROM table0, table1 AS alias1, table2 AS alias2
    WHERE table0.link0 = alias1.id AND table1.link1 = alias2.id AND ...
                                       ^^^^^^
                             should be alias1

This fixes the issue, and adds a test to reproduce it.  The fix is based on
@emiprotechnologies's own proposal, but is cleaner and does not break APIs.
2015-05-12 13:42:27 +02:00
Denis Ledoux b7f1b9c01e [FIX] models: recompute nested old-style computed field
The `set` method of the one2many class returns a list
of the fields that require recomputation,
the computing of the function fields being delayed
for performances reasons.

Nevertheless, if the `set` method was called
through another `set` method, in other words,
nested `set` calls, the fields to recompute returned
by the second, nested, call to set were never recomputed,
the result list were simply lost.

e.g.:
```
create/write
│set
└─── create/write with recs.env.recompute set to False
    │set
        └─── create
             with recs.env.recompute set to False
```

To overcome this problem, the list of old api style
compute fields to recompute is stored
within the environment, and this list is cleared
each time the store_set_value has done its job of
recomputing all old api style compute fields.

opw-629650
opw-632624
closes #6053
2015-05-06 17:25:44 +02:00
Raphael Collet 1a535b9d87 [IMP] models: in recompute, do not store fields that should not be recomputed
Consider a new field that uses the same compute method as another existing
field.  When the field is introduced in database, its value must be computed on
existing records.  In such a case, the existing field should not be written, as
its value is not supposed to have changed.  Not writing on the existing field
can avoid useless recomputations in cascade, which is the reason we introduce
this patch.
2015-05-06 15:24:39 +02:00
Raphael Collet 0c665edb66 [FIX] models: make sure an environment is available when we access field.digits
Accessing `field.digits` can crash if no environment is available at that
point.  This happens in function `get_pg_type()`, which is called from method
`_auto_init()`.  An environment is simply created in the method's scope to be
available for `field.digits`.
2015-05-05 17:33:16 +02:00
Anthony Muschang 2925bb5c9a [FIX] models: Always use MissingError when a document is missing
As done in write and already in next version (see 0fd773a), accessing a deleted
record (through read or check access rights) should always return a MissingError
instead of the generic except_orm.
This will allow code ignoring deleted record (e.g. 'recompute' method) to safely
process the other records.
Fixes #6105
2015-05-04 13:39:21 +02:00
Sylvain Calador fa18530210 [REF] Add a hook for import fake fields
Used during records extraction phase of import (CSV)

closes #6188
2015-04-29 10:42:56 +02:00
Christophe Simonis 14fd77c132 [FIX] core: ormcache is now per regisry.
A cross-registry cache was introduced by e2ea691ce.
The initial idea was praiseworthy but sub-optimal for servers with a
lot of registries.
When there is lot of registry loaded, the cache size was huge and
clearing some entries took a lot of CPU time, increasing the chances
of timeout.

Also, the cache was not cleaned when a registry is removed from
registry LRU (this operation would also consume time).
2015-04-03 16:11:57 +02:00
Raphael Collet 9a365e83e7 [FIX] models: do not prefetch fields while the model schema is created
The following case has shown the issue: extend the model `res.company` by
adding at least two fields F and G, where F has a default value defined as:

    lambda self: self.env.user.company_id.name

If the column F is created before G in the database, the existing records will
be filled with the default value of F.  When the default value is computed, the
field `name` from a `res.company` is read, and other fields are prefetched,
including G.  This operation fails, because G does not exist in database yet!
2015-04-02 16:54:39 +02:00
Raphael Collet 93c341a52c [IMP] models: avoid using `map` instead of a `for` loop (thanks chs@odoo.com) 2015-03-23 14:36:15 +01:00
Raphael Collet f93a14e3b2 [IMP] fields: remove comments about `_all_columns`, and improve its docstring 2015-03-23 14:36:15 +01:00
Raphael Collet ffa7f28d34 [IMP] fields: reduce memory footprint of list/set field attributes
The optimization consists in using tuples for attributes `inverse_fields`,
`computed_fields` and `_triggers`, and to let them share their value when it is
empty, which is common.  This saves around 1.8Mb per registry.
2015-03-23 14:36:15 +01:00
Raphael Collet 9aad3d873b [IMP] fields: turn field.digits and column.digits into dynamic properties
The computed value of parameter digits is no longer stored into fields and
columns; instead the value is recomputed everytime it is needed.  Note that
performance is not an issue, since the method `get_precision` of model
'decimal.precision' is cached by the orm.  This simplifies the management of
digits on fields and saves about 300Kb per registry.
2015-03-23 14:36:15 +01:00
Raphael Collet bf703fd9a3 [FIX] models: reorder the base classes of models following an equivalent class hierarchy
Sometimes, the expected mro of the model is not the same as the one built with
a binary class hierarchy.  So we reorder the base classes in order to match the
equivalent binary class hierarchy.  This also fixes the cases where duplicates
appear in base classes.
2015-03-23 14:36:14 +01:00
Raphael Collet 9bce04de79 [IMP] models: build a flat hierarchy of classes for models
Instead of composing classes in a binary tree hierarchy, we make one class that
inherits from all its base classes.  This avoids keeping intermediate data
structures for columns, inherits, constraints, etc.  This saves about 600Kb per
registry.
2015-03-23 14:36:14 +01:00
Raphael Collet 0f9b452c33 [IMP] models: convert deprecated model._all_columns into a dynamic property
The mappings model._all_columns takes quite some memory (around 2MB per
registry) because of the numerous dictionaries (one per model) and inefficient
memory storage of column_info.  Since it is deprecated and almost no longer
used, it can be computed on demand.
2015-03-23 14:36:14 +01:00
Raphael Collet e2ea691cef [IMP] ormcache: turn it into a global LRU cache shared among registries
The ormcache is now shared among registries.  The cached methods use keys like
(DBNAME, MODELNAME, METHOD, args...).  This allows registries with high load to
use more cache than other registries.
2015-03-23 14:36:14 +01:00
Raphael Collet 6aa28a89bb [FIX] models: compute many2many field on existing records when field is introduced by an update 2015-03-18 09:43:45 +01:00
Thomas Groutars 3923f4051d [FIX] models: propagate context in action returned by get_formview_action
When opening a record from a many2one,
the context is not propagated to fields_view_get.
This is a problem if you set "form_view_ref" in the context for example.

opw-629628
2015-03-12 15:13:44 +01:00
Julien Laloux a20545a936 [IMP] web: add no update info on metadata view (debug mode) 2015-03-11 11:10:42 +01:00
Raphael Collet 04ba0e99a4 [IMP] models: in _read_from_database(), fetch inherited fields when possible
This should improve the performance of method read() on models with inherited
fields, like product.product.  The inherited fields that are stored as columns
in parent tables (except for translated fields) are read in the same query as
the fields of the model.  Those fields will be directly stored in cache under
the main model, so that no copying will take place in cache for accessing them
(this is the default implementation of inherited fields).
2015-03-03 13:18:04 +01:00
Raphael Collet 2c261a2987 [IMP] models: in _read_from_database(), use the Query object to build the query
This makes the query construction more robust, as it handles joins for
conditions and ORDER BY clauses.  It also makes it easier to read() from
several tables (like inherited fields).
2015-03-03 13:18:04 +01:00
Christophe Simonis e6396deb4e [IMP] models: log source when "Comparing apples and oranges" 2015-02-27 15:57:13 +01:00
Raphael Collet 0c1b95d824 Merge pull request #5439 from guewen/test-inherits-3-levels
Reload fields of parent inherits, fixes #5398
2015-02-26 10:06:31 +01:00
Guewen Baconnier 608e58cce1 Reload fields of parent inherits, fixes #5398 2015-02-24 10:34:09 +01:00
Christophe Simonis a6a1764b2c [FIX] models: initialize `_fields` attribute.
Custom fields can point to custom models that have not been initialized
yet (`_setup_base` not called). Ensure every models in the registry
have a `_fields` attribute.
Use a `frozendict` as a defensive check to ensure it wont be modified
before calling `_setup_base`.
2015-02-23 18:33:26 +01:00
Raphael Collet 121b8e6800 [FIX] models: in onchange, false changes where detected in many2many fields
This was due to secondary fields loaded from database in 'onchange' mode.  In
that mode, the secondary fields were marked 'dirty', and therefore returned by
the method `onchange`.  The fix consists in loading those secondary fields in
cache before processing the onchanges.

This incidentally fixes a test on method `onchange`: in a one2many field, some
dirty fields were unexpectedly returned in the result.  This was due to those
fields being loaded while processing onchanges.
2015-02-21 11:29:13 +01:00
Raphael Collet 9333c622e9 [FIX] fields: allow overriding of a old-api function field
When overriding a field defined as a function field, the field must either
create a corresponding column that is not a fields.function (if stored), or
have no corresponding column (if not stored).
2015-02-19 15:33:27 +01:00
Raphael Collet bf99f434c8 [IMP] models: speed up loading by computing `_constraint_methods` and `_onchange_methods` lazily 2015-02-18 16:43:20 +01:00
Raphael Collet 45a37b22fd [IMP] models: speedup loading of registry (-20%)
Idea: look up for the model's fields in method `_setup_base()` instead of
method `__init__()`.  This does not make a significant difference when
installing or upgrading modules, but when simply loading a registry, the
(expensive) field lookup is done once per model instead of once per class.
2015-02-18 16:43:20 +01:00
Denis Ledoux c9154e08aa [FIX] api: environment recomputation
In a workflow context (for instance, in the invoice workflow),
context is not passed.

Therefore, relying on the 'recompute' key being the context
in order to not recompute the fields does not work with Workflows.

It leads to huge performance issues,
as fields are recomputed recursively (instead of sequentially)
when several records are implied.
For instance, when reconciling several invoices with one payment
(100 invoices with 1 payment for instance),
records of each invoice are recomputed uselessly in each workflow call
(for each "confirm_paid" method done for each invoice).

With a significant number of invoices (100, for instance),
it even leads to a "Maximum recursion depth reached" errror.

closes #4905
2015-02-12 14:57:31 +01:00
Raphael Collet 75ca4f8b71 [FIX] models: in onchange(), do not assign field 'id' on record 2015-02-11 12:45:18 +01:00
Raphael Collet 3a44d84b0f [FIX] models: inherited fields must be copied iff their original field is copied
This fixes a bug introduced by commit f650522bbf
(related fields should not be copied by default).  Inherited fields are a
particular case, and given the implementation of copy(), they must be copied if
their original field is copied.

The test on copy() in test_orm has been modified to show the bug.
2015-02-11 12:23:11 +01:00
Raphael Collet ac0eec6c99 [IMP] models: in recompute(), only save stored fields to database 2015-02-11 10:49:17 +01:00
Raphael Collet 6cf7bc8838 [FIX] models: invoke old-api onchange methods with context
This helps fixing old-api onchange methods with a record id as a parameter.
Browsing this record id may be problematic, since it reads the record in an
environment with an empty context.  This is really problematic when the record
is a new record, because such a record only exists in a given environment.
2015-02-11 09:29:51 +01:00
Raphael Collet 2067a206ec [FIX] models: process onchange methods on new records in the order of the view
The onchange() on new records processes fields in non-predictable order.  This
is problematic when onchange methods are designed to be applied after each
other.  The expected order is presumed to be the one of the fields in the view.

In order to implement this behavior, the JS client invokes method onchange()
with the list of fields (in view order) instead of False.  The server then uses
that order for evaluating the onchange methods.

This fixes #4897.
2015-02-10 13:12:40 +01:00
Christophe Simonis 7ab70cab74 [IMP] models.py: do not log handled "bad query" 2015-02-04 14:45:50 +01:00
Csaba Tóth ac845819ee [FIX] models: allow empty selection list
If a selection field is created with an empty list of choices (e.g. added by
submodules), initialise the field as a varchar column (most common).
Check if the list exists to avoid crashing while checking the type of the first
key.
Fixes #3810
2015-02-02 10:54:04 +01:00
Olivier Laurent c49d20c61a [FIX] base: _auto_init change column type fix
When changing the type of a column (if size differs for example),
'selection' field should be considered like a 'char' field (since they
are internaly the same column type)

This will fix some migration issues where 'char' fields were correctly
changed but not 'selection,' field.

Use case:
* create a 6.0 db with 'stock' module installed
* 'state' field in 'stock.move' model is of type 'character varying(16)'
* migrate it to 8.0
* 'state' field is still 'character varying(16)' but should normally be
  'character varying'
2015-01-29 14:16:21 +01:00
Denis Ledoux dcfd94cbf5 [FIX] orm: Revert 332154444d && acd7d84da4
These revs. introduced an API change in the _name_search method.

Indeed, the 'operator' attribute used to have 'ilike' as default value.
This cannot be changed, as every modules overriding this method
overrided it using the signature with operator='ilike'

For instance, _name_search method of addons/base/ir/ir_model.py
expects having 'ilike' as operator.
As it was not anymore the case,
it leaded to a crash when performing a name_search call on the model ir.model,
like when adding a new custom field to a model, from the web client.

opw-626161
2015-01-21 18:33:36 +01:00
Raphael Collet 431f8de815 [IMP] models: prepare the setup of fields at one place only 2015-01-21 11:26:23 +01:00
Raphael Collet 54c655cb71 [FIX] models: do not introduce a one2many manual field if its inverse is not on its comodel 2015-01-21 11:26:23 +01:00
Raphael Collet cf26f7ed80 [IMP] models, registry: let the registry retrieve manual fields from database 2015-01-21 11:26:23 +01:00
Raphael Collet 6c29af3fa5 [FIX] models: init function fields once columns are known and not before 2015-01-21 11:26:23 +01:00
Raphael Collet 5fee95ca63 [FIX] models, fields: reorganize model setup to retrieve all inherited fields
The model setup sometimes misses entries in _inherit_fields and _all_columns.
This is because those dictionaries are computed from parent models which are
not guaranteed to be completely set up: sometimes a parent field is only
partially set up, and columns are missing (they are generated from fields after
their setup).

To avoid this bug, the setup has been split in three phases:
(1) determine all inherited and custom fields on models;
(2) setup fields, except for recomputation triggers, and generate columns;
(3) add recomputation triggers and complete the setup of the model.

Making these three phases explicit brings good invariants:
- when setting up a field, all models know all their fields;
- when adding recomputation triggers, you know that fields have been set up.
2015-01-21 11:26:23 +01:00
Raphael Collet 2f06adde9c [IMP] models: speedup registry loading (35% less time)
The field setup on models is improved: only fields are determined when building
the model's class; the final _columns is computed from the fields once they are
set up.
2015-01-21 11:26:23 +01:00
Samus CTO acd7d84da4 [IMP] make name_search() use '=' if col _rec_name is an integer
When looking for an issue number, it's more useful to match the word in
the whole string instead of just a part of it.
2015-01-20 09:52:55 +01:00
Denis Ledoux 87dd06c941 [MERGE] forward port of branch saas-3 up to 8c150c6 2015-01-15 14:43:32 +01:00
Raphael Collet ec25207b8f [IMP] models: force recomputation of all fields on a newly created record 2015-01-13 16:03:58 +01:00
Denis Ledoux be5717434e [FIX] fields: apply user timezone in display name for models using a datetime as name
For models using a datetime field as name (hr.attendance for instance), the user timezone wasn't applied in the display name.
Therefore, in the breadcrumb, the datetime was different than in the form if the user had another timezone than UTC.

This rev. is related to 27d8cb843b, but is for the 8.0 api

We are aware we introduce a tiny change of API (method signature change), which we normally prohibit, but considering the really low level of the method, the fact it is probably not ovveriden by any other modules and the fact there is no cleaner way to correct this, we are making an exception.
2015-01-05 15:48:37 +01:00
Martin Trigaux 33c8aae8e4 [FIX] orm: do not aggregate non-stored columns
Columns defined in the new api as interger, computed and non-stored should not be aggregated in read_group.
Fallback on False if column is None
Fixes #3972, opw 619536
2015-01-05 11:30:26 +01:00