When creating a table with SQLObject, it generates a primary key ID that is not in the Class definition ( ex Person and Address) This ID must be used to point to a related table ( foreign key) As far as i know (beginner for SQLObject i see no way to get that info in a classmethod ( addAddress in the Person class) Is it possible to get the ID without explicit GET.
I include the source code (based on example in SQLOject) to clarify:
from sqlobject import *
import sqlite3
import os
from sqlobject import *
db_filename =os.path.abspath('test.db')
connection_string = 'sqlite:' + db_filename
connection = connectionForURI(connection_string)
sqlhub.processConnection = connection
class Person(SQLObject):
firstName = StringCol()
lastName = StringCol()
addresses = MultipleJoin('Address')
# instead of passing the parameter id, is it possible
# to obtain the id of the database record
# the other info from thta class is accesible ( see print name)
@classmethod
def addAddress(cls,address,id):
# i added id as aparameter but i would like to avoid that.
# is it possible to have the following
# data (no error but reference only ?)
print(cls.firstName)
# for id it generates an error
# error Person has no attribute id
# print(cls.id)
Address(street = address.street
,city=address.city
,state=address.state
,zip=address.zip
,person=id)
Person.createTable(ifNotExists=True)
class Address(SQLObject):
street = StringCol()
city = StringCol()
state = StringCol(length=2)
zip = StringCol(length=9)
person = ForeignKey('Person')
Address.createTable(ifNotExists=True)
Person(firstName="John", lastName="Doe")
p = Person.get(1)
a=Address(street='123 W Main St', city='Smallsville',
state='MN', zip='55407', person=p)
p.addAddress(a,p.id)