Goodmorning, I've a DB with three tables :

db.define_table('person',
            Field('name', length=100),
            Field('dob', length=10),
            Field('address', 'text', length=255),
            Field('countryname', length=3),
            Field('statename', length=100),
            )

db.define_table('opslist',
            Field('opid', length=10),
            Field('dop', length=10),
            Field('type_operation', length=25),
            )

db.define_table('cardlist',
            Field('opid', db.opslist),
            Field('name', db.person),
            Field('countryname', length=3, required=True),
            Field('statename', length=100, required=True),
            Field('zip', length=5, required=False),
            )

I would insert by code, means not using a sqlform or a form, some data into ... e.g.: If i have all the values insertable into cardlist table the operation must give me the ability to fill automatically the related record (creating new if not present) into the other tables. This because it's a relational db with foreign keys ...

Is this possible with web2py using the DAL ? or do i have to write my raw sql command ?

Thank You All

有帮助吗?

解决方案

Just use the insert method. It returns the ID of the newly created record, which you can use to insert into the reference fields:

person = db.person.insert(name=..., dob=..., ...)
opslist = db.opslist.insert(opid=..., dop=..., ...)
db.cardlist.insert(opid=opslist, name=person, ...)
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top