Failed to change the "id" column into "_id"
Opened this issue · 4 comments
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:root@127.0.0.1:3306/webscraping?charset=utf8')
db.create_tables()
Thanks,
Qi
Yes, it is hard-coded somewhere.
I will try to change it once I have more time.
The core code for quick_orm has only 200+ lines, if you want, you can read
it and try to figure out a solution.
Best Regards,
Tyler Long
On Thu, Feb 21, 2013 at 5:15 PM, pengqi notifications@github.com wrote:
Hello,
I tried to change the "id" column to "_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_ormfrom quick_orm.core import MyDeclarativeMeta, Database, StringUtil, modelsfrom 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:root@127.0.0.1:3306/webscraping?charset=utf8')
db.create_tables()Thanks,
Qi—
Reply to this email directly or view it on GitHubhttps://github.com//issues/9.
Best Regards,
Tyler Long
Home page: http://www.tylerlong.me
Github: https://github.com/tylerlong
Bitbucket: https://bitbucket.org/tylerlong http://bitbucket.org/tylerlong
Compared to sqlalchemy, quick_orm provides simplicity, but loses
flexibility.
It was designed according to my own programming experience, so I ignored
some rare cases. Currently I am not programming with python, I will try my
best to find some time to review and rewrite quick_orm.
On Fri, Feb 22, 2013 at 4:52 PM, Tyler Long tyler4long@gmail.com wrote:
Yes, it is hard-coded somewhere.
I will try to change it once I have more time.
The core code for quick_orm has only 200+ lines, if you want, you can read
it and try to figure out a solution.Best Regards,
Tyler Long
On Thu, Feb 21, 2013 at 5:15 PM, pengqi notifications@github.com wrote:
Hello,
I tried to change the "id" column to "_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_ormfrom quick_orm.core import MyDeclarativeMeta, Database, StringUtil, modelsfrom 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:root@127.0.0.1:3306/webscraping?charset=utf8')
db.create_tables()Thanks,
Qi—
Reply to this email directly or view it on GitHubhttps://github.com//issues/9.Best Regards,
Tyler Long
Home page: http://www.tylerlong.me
Github: https://github.com/tylerlong
Bitbucket: https://bitbucket.org/tylerlonghttp://bitbucket.org/tylerlong
Best Regards,
Tyler Long
Home page: http://www.tylerlong.me
Github: https://github.com/tylerlong
Bitbucket: https://bitbucket.org/tylerlong http://bitbucket.org/tylerlong
Thanks.
I have added the feature: The "id" filed name can be user-defined now.
https://github.com/pengqi/quick_orm/blob/master/quick_orm/core.py
See following example:
# 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
__metaclass__ = Database.DefaultMeta
class Users :
__id__ = 'user_id'
last_name = Column(String(50))
first_name = Column(String(50))
@Database.foreign_key(Users, ref_name='author', backref_name='posts')
class Posts :
__id__ = 'post_id'
title = Column(String(200))
content = Column(Text)
Database.register()
if __name__ == '__main__':
db = Database('mysql://root:root@127.0.0.1:3306/webscraping?charset=utf8')
db.create_tables()
user = Users()
user.last_name = 'Qi'
user.first_name = 'Peng'
db.session.add_then_commit(user)
post = Posts()
post.author = user
post.title = 'Life is short.'
post.content = 'You need Python.'
db.session.add_then_commit(post)
Thank you so much!
I will integrate your code into the quick_orm after reviewing and testing.
On Tue, Mar 5, 2013 at 2:09 PM, pengqi notifications@github.com wrote:
Thanks.
I have added the feature: can custom the "id" filed name now.
https://github.com/pengqi/quick_orm/blob/master/quick_orm/core.pySee following example:
coding: utf-8# test.py
https://github.com/tylerlong/quick_orm# pip install quick_ormfrom quick_orm.core import MyDeclarativeMeta, Database, StringUtil, modelsfrom sqlalchemy import Column, String, Text
metaclass = Database.DefaultMeta
class Users :
id = 'user_id'
last_name = Column(String(50))
first_name = Column(String(50))
@Database.foreign_key(Users, ref_name='author', backref_name='posts') class Posts :
id = 'post_id'
title = Column(String(200))
content = Column(Text)
Database.register()
if name == 'main':
db = Database('mysql://root:root@127.0.0.1:3306/webscraping?charset=utf8')
db.create_tables()user = Users() user.last_name = 'Qi' user.first_name = 'Peng' db.session.add_then_commit(user) post = Posts() post.author = user post.title = 'Life is short.' post.content = 'You need Python.' db.session.add_then_commit(post)
[image: sample]https://f.cloud.github.com/assets/1720794/220104/29a31d74-855b-11e2-98b7-502249baf395.png
—
Reply to this email directly or view it on GitHubhttps://github.com//issues/9#issuecomment-14425183
.
Best Regards,
Tyler Long
Home page: http://www.tylerlong.me
Github: https://github.com/tylerlong
Bitbucket: https://bitbucket.org/tylerlong http://bitbucket.org/tylerlong