On Tue, Aug 21, 2012 at 7:58 AM, Alexei Vinidiktov <
[email protected]> wrote:

>
>
> On Tue, Aug 21, 2012 at 7:18 AM, Alan Etkin <[email protected]> wrote:
>
>> El lunes, 20 de agosto de 2012 21:04:46 UTC-3, Alexei Vinidiktov escribió:
>>
>>> This web2py group thread may be of help:
>>>
>>> https://groups.google.com/**forum/?fromgroups=#!topic/**
>>> web2py/0X3ykXLmcEQ<https://groups.google.com/forum/?fromgroups=#%21topic/web2py/0X3ykXLmcEQ>
>>>
>>>
>>> Also see issue:  
>>> http://code.google.com/p/**web2py/issues/detail?id=796<http://code.google.com/p/web2py/issues/detail?id=796>
>>>  If
>>> I understand it correctly, it has been implemented in trunk.
>>>
>>>
>> So re-editing import_csv_.. does not seem the way to go. Would you think
>> that It's possible to call import_... with id_map={} for a clean pg
>> database to keep old ids?
>>
>>
> As far as I understand id_map={} only allows you to maintain referential
> integrity but it doesn't present the original ids.
>
> Whereas the solution proposed in issue http://code.google.com/p/**
> web2py/issues/detail?id=796<http://code.google.com/p/web2py/issues/detail?id=796>
>  should
> allow to preserve the original ids.
>
> Quote:
> "
> PostgreSQL:
>
> ...import rows...
> count = db.executesql('SELECT MAX(id) FROM mytable;')
> count += 1
> db.executesql('ALTER SEQUENCE mytable_sequence RESTART WITH %s;' % count)
>
> MySQL and PostgreSQL should both let you insert manual ID values. You just 
> have to reset the auto-increment value so that new rows after import get 
> proper IDs. With MSSQL, you have to do the import as a transaction, turning 
> on the ability to manually insert ID values, then importing, and finally 
> telling it to automatically recalculate the auto-increment values.
>
> "
>
> I haven't tried it myself though. In the issue tracker issue 796 is marked
> as fixed, but there is no explanation of how it was fixed.
>
> I hope Massimo will chime in.
>
>
Here's a part of the function import_from_csv_file in trunk dal.py. Judging
by its description it appears to provide the ability to preserve the ids.

def import_from_csv_file(
        self,
        csvfile,
        id_map=None,
        null='<NULL>',
        unique='uuid',
        id_offset=None, # id_offset used only when id_map is None
        *args, **kwargs
        ):
        """
        Import records from csv file.
        Column headers must have same names as table fields.
        Field 'id' is ignored.
        If column names read 'table.file' the 'table.' prefix is ignored.
        'unique' argument is a field which must be unique
            (typically a uuid field)
        'restore' argument is default False;
            if set True will remove old values in table first.
        'id_map' ff set to None will not map ids.
        The import will keep the id numbers in the restored table.
        This assumes that there is an field of type id that
        is integer and in incrementing order.
        Will keep the id numbers in restored table.
        """

        delimiter = kwargs.get('delimiter', ',')
        quotechar = kwargs.get('quotechar', '"')
        quoting = kwargs.get('quoting', csv.QUOTE_MINIMAL)
        restore = kwargs.get('restore', False)
        if restore:
            self._db[self].truncate()

    [...]


-- 
Alexei Vinidiktov

-- 



Reply via email to