Re: [SQLObject] bug with sqlmeta.idName and non autoincrement primary keys?
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
From: Ian B. <ia...@co...> - 2005-11-09 17:24:14
|
Vladimir Blagojevic wrote: > Hi, > > if I look at the code where the exception is raised (col.py, def > from_python(self, *value*, state)) I see the following check: > > if not isinstance(*value*, (int, long, sqlbuilder.SQLExpression)): > raise validators.Invalid("expected an int in the IntCol > '%s', got %s instead" % \ > (self.name, type(value)), value, state) > > The problem here is that 'value' is '6', which is a string. Where does > the string come from? It comes from mysqlconnection.py: > > def columnsFromSchema(self, tableName, soClass): > colData = self.queryAll("SHOW COLUMNS FROM %s" > % tableName)... > > MySql returns: > > (('person_id', 'int(11)', '', 'PRI', '6', ''), ('name', 'varchar(50)', > '', '', '', '')) > > ---> '6' is the default value for column person_id Ah... I think MySQL is returning a SQL literal, not an actual value. I'm not sure what the best way to deal with that is; we don't exactly have a SQL literal evaluator here. Though technically we could evaluate "SELECT 6"; that's probably the right answer, actually. Unless the default is, say, "NOW()", at which point we'll have made the default fixed where it should be dynamic... what would be ideal would be a sqlbuilder expression, which is broken now, but if it wasn't broken it would mean SQLObject would automatically expire objects that had inserts or updates that used a sqlbuilder expression, so the actual value was fetched on first access. Though in this case the column object shouldn't be created at all, which is the problem effecting your particular case. > The other question is, why is the default value for person_id passed > as the value to from_python. That comes from the _create method in > SQLObject: > ... > if not kw.has_key(column.name) and not > kw.has_key(column.foreignName): > default = column.default > ... > > > One thing I don't understand, why would we set persionId to the > default value when we have passed id=22 in the constructor. Because SQLObject is horribly confused. It doesn't think of the primary key column like other columns, it's special. It apparently also doesn't check for conflicts -- if you add two columns with the same name you get an error. But if you add a column with the same name as the primary key, you don't. But you should; there should be no personID column on your class. Probably the problem is that the fromDatabase code should be ignoring person_id. -- Ian Bicking / ia...@co... / http://blog.ianbicking.org |