-
Notifications
You must be signed in to change notification settings - Fork 9
Open
Description
Hello,
I tried to change the "id" column into "_id", but failed. The exception is as following:
sqlalchemy.exc.NoReferencedColumnError: Could not create ForeignKey 'users.id' on table 'posts': table 'users' has no column named 'id'
It seems there is other hard-coded in the library.
The code is as following:
# coding: utf-8
# test.py
# https://github.com/tylerlong/quick_orm
# pip install quick_orm
from quick_orm.core import MyDeclarativeMeta, Database, StringUtil, models
from sqlalchemy import Column, String, Text, Integer, DateTime
class myMeta(MyDeclarativeMeta):
"""metaclass for all model classes, let model class inherit Database.Base and handle table inheritance.
All other model metaclasses are either directly or indirectly derived from this class.
"""
def __new__(cls, name, bases, attrs):
# add Database.Base as parent class
bases = list(bases)
if object in bases:
bases.remove(object)
bases.append(Database.Base)
seen = set()
bases = tuple(base for base in bases if not base in seen and not seen.add(base))
attrs['__tablename__'] = StringUtil.camelcase_to_underscore(name)
print attrs['__tablename__']
attrs['_id'] = Column(Integer, primary_key = True)
attrs['created_at'] = Column(DateTime)
attrs['updated_at'] = Column(DateTime)
attrs['_readable_name'] = attrs['__tablename__']
attrs['_readable_names'] = attrs['_readable_name'] + 's'
attrs['_one_to_models'] = attrs['_many_to_models'] = []
if not '__mapper_args__' in attrs:
attrs['__mapper_args__'] = {}
# the for loop bellow handles table inheritance
for base in [base for base in bases if base in models]:
if not hasattr(base, 'real_type'):
base.real_type = Column('real_type', String(24), nullable = False, index = True)
base.__mapper_args__['polymorphic_on'] = base.real_type
base.__mapper_args__['polymorphic_identity'] = base._readable_name
attrs['_id'] = Column(Integer, ForeignKey('{0}._id'.format(base._readable_name), ondelete = "CASCADE"), primary_key = True)
attrs['__mapper_args__']['polymorphic_identity'] = attrs['_readable_name']
attrs['__mapper_args__']['inherit_condition'] = attrs['_id'] == base._id
return MyDeclarativeMeta.__new__(cls, name, bases, attrs)
__metaclass__ = myMeta
class Users :
last_name = Column(String(50))
lfirst_name = Column(String(50))
@Database.foreign_key(Users, ref_name='author', backref_name='posts')
class Posts :
title = Column(String(200))
content = Column(Text)
Database.register()
if __name__ == '__main__':
db = Database('mysql://root:[email protected]:3306/webscraping?charset=utf8')
db.create_tables()Thanks,
Qi
Metadata
Metadata
Assignees
Labels
No labels