Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.Sign up
Wrong FK column type when using `sql_type` on foreign ID column #74
When an entity
Before anything else, I acknowledge that what I was trying to do is a terrible hack and might even be unusable when actually managing the objects. Still, the generation part is problematic here, as even if doing so is not correct, mismatching FK column types are generated without warnings / errors.
from pony.orm import * sql_debug(True) db = Database('mysql', db='ponybug', host='localhost', user='root') class Foo(db.Entity): id = PrimaryKey(unicode, sql_type='INTEGER', auto = True) name = Optional(unicode) bars = Set(lambda: Bar) class Bar(db.Entity): foo = Required(Foo) name = Optional(unicode) db.generate_mapping(create_tables=True)
Excerpt from the debug output:
CREATE TABLE `foo` ( `id` INTEGER PRIMARY KEY AUTO_INCREMENT, `name` VARCHAR(255) NOT NULL ) CREATE TABLE `bar` ( `id` INTEGER PRIMARY KEY AUTO_INCREMENT, `foo` VARCHAR(255) NOT NULL, `name` VARCHAR(255) NOT NULL )
`foo` VARCHAR(255) NOT NULL
where it should have been
`foo` INTEGER NOT NULL
Explicitly specifying the underlying type of the foreign key solves the problem:
class Bar(db.Entity): foo = Required(Foo, sql_type='INTEGER') ...
Thanks, fixed. Note that for MySQL, when the PK column of the main table has the
For PostgreSQL, when the PK column of the main table has the SQL type