import_from_csv_file and auth.signature

42 views
Skip to first unread message

David Orme

unread,
Jul 24, 2020, 6:30:05 AM7/24/20
to web2py-users
Hello,

I've got a table like this:

db._common_fields.append(auth.signature)

db
.define_table('locations',
               
Field('title', 'string'),
               
Field('capacity', 'integer'),
               
Field('celcat_code', 'string'),
               
Field('is_external', 'boolean', default=False),
                format
= lambda row: f"{row.title}")

I'm trying to populate that with legacy data using:

    filepath = os.path.join(request.folder, 'static', 'data', 'teaching_staff.csv')
   
   
with open(filepath, encoding="utf8") as csvfile:
        db
.teaching_staff.import_from_csv_file(csvfile)

And I get a ticket:

IntegrityError(FOREIGN KEY constraint failed)

I think what is going on is that - because no-one is logged in when that import is run - the `auth.signature` fields are None and so that is the constraint that is failing. Is there a canonical way to handle this? I had a quick look at the code and couldn't see an option for inserting default values. I can write a loop and add default values myself to individual record inserts but is there a standard way?

Many thanks,
David

villas

unread,
Jul 24, 2020, 7:46:12 AM7/24/20
to web2py-users
Did you try...

db.locations.created_by.default = 1 # or whatever
db.locations.modified_by.update = 1

prior to: import_from_csv_file


David Orme

unread,
Jul 24, 2020, 9:31:53 AM7/24/20
to web2py-users
Well now that is embarrassing. Thanks!

For reference, this was my recipe to put at the top of a file that is going to populate multiple tables:

if db(db.auth_user).count() == 0:
   
#Bulk load account
    admin_user_id
= db.auth_user.insert(first_name='Bulk uploader')
else:
    admin_user_id
= db(db.auth_user.first_name == 'Bulk uploader').select().first().id

for table in ['table','names']:
    table_object
= db[table]
    table_object
.created_by.default = admin_user_id
    table_object
.modified_by.default = admin_user_id
Reply all
Reply to author
Forward
0 new messages