Package orm2 :: Package adapters :: Package firebird :: Module datatypes :: Class serial
[hide private]
[frames] | no frames]

Class serial

source code

    object --+            
             |            
      property --+        
                 |        
datatypes.datatype --+    
                     |    
     datatypes.integer --+
                         |
                        serial

Datatype for automatically gernerated ids.

From The FireBird FAQ

How can i make an auto-incrementing primary key column? and is the NOT NULL constraint mandatory for a primary key?

In Firebird, you achieve an auto-incrementing PK using a generator and a BEFORE INSERT trigger:
  CREATE GENERATOR GEN_PK_ATABLE;
  COMMIT;

Define the column, e.g., ATABLE_ID, as BIGINT or INTEGER and yes, the NOT NULL constraint is mandatory for a primary key.

The trigger for automatic population of the key is this:
  CREATE TRIGGER BI_ATABLE FOR ATABLE
  ACTIVE BEFORE INSERT
  AS
  BEGIN
    IF(NEW.ATABLE_ID IS NULL) THEN 
    NEW.ATABLE_ID = GEN_ID(GEN_PK_ATABLE, 1);
  END
This is NOT what orm does! It does...

How can I get the value of my generated primary key after an insert ?

Firebird doesn't currently return values from insert statements and, in multi-user, it isn't safe to query the generator afterwards (GEN_ID(Generator_name, 0) because you have no way to know whether the current 'latest' value was yours or someone else's. The trick is to get the generator value into your application as a variable before you post your insert. This way, you make it available not just for your insert but for any dependent rows you need to create for it inside the same transaction:
  SELECT GEN_ID(Generator_name, 1) AS MyVar FROM RDB$DATABASE;
Some data access interfaces and drivers provide ways to do this automatically for you. For example, IB Objects implements the GeneratorLinks property for statement classes.

How it is handeld by orm

orm is the kind of interface that does this automatically for you. For any serial (and in fact common_serial, see above) column it will query the corresponding sequence and store the resulting value in the right attribute to be used in the INSERT statement. If you use a serial as your primary key, the select_after_insert() mechanism (see orm2.datasource.datasource) will work correctly.

Nested Classes [hide private]

Inherited from datatypes.integer: python_class, sql_literal_class


Instance Methods [hide private]
  __init__(self, column=None, sequence=None, title=None, validators=(), widget_specs=())
  __init_dbclass__(self, dbclass, attribute_name)
This methods gets called by dbobject's metaclass.
  __set__(self, dbobj, value)
Set the attribute managed by this datatype class on instance to value.

Inherited from datatypes.datatype: __convert__, __delete__, __get__, __select_after_insert__, __select_this_column__, __set_from_result__, __setattr__, add_widget, check_dbobj, data_attribute_name, isset, sql_literal, widget_specs

Inherited from property: __getattribute__, __new__

Inherited from object: __delattr__, __hash__, __reduce__, __reduce_ex__, __repr__, __str__


Class Variables [hide private]

Inherited from property: fdel, fget, fset


Properties [hide private]

Inherited from object: __class__


Method Details [hide private]

__init__(self, column=None, sequence=None, title=None, validators=(), widget_specs=())
(Constructor)

source code 
Parameters:
  • sequence - Either a string or a sql.identifyer instance, naming the sequence to be used by this serial column. Defaults to GEN_PK_<relation name>
Overrides: datatypes.datatype.__init__

__init_dbclass__(self, dbclass, attribute_name)

source code 
This methods gets called by dbobject's metaclass. It supplies the db property with info about the class it belongs to and its attribute name.
Overrides: datatypes.datatype.__init_dbclass__
(inherited documentation)

__set__(self, dbobj, value)

source code 
Set the attribute managed by this datatype class on instance to value. This will be called by Python on attribute assignment. The __set_from_result__ method does the same thing for data retrieved from the RDBMS. See below.
Overrides: datatypes.datatype.__set__
(inherited documentation)