Python SQLAlchemy Basic Structure -
i have been watching few videos , reading various posts try , understand basic structure of sqlalchemy in python script.
can tell me:
- why below script fails
script:
from sqlalchemy import * sqlalchemy.ext.declarative import declarative_base base = declarative_base() class records(base): __tablename__ = 'records' data1 = column(text()) data2 = column(text()) data3 = column(text()) data4 = column(text()) if __name__ == "__main__": engine = create_engine('mysql://user:password@localhost/database') base.metadata.create_all(bind=engine, checkfirst=true) it fails with
traceback (most recent call last): file "sqltest.py", line 6, in <module> class records(base): file "build/bdist.linux-x86_64/egg/sqlalchemy/ext/declarative/api.py", line 55, in __init__ file "build/bdist.linux-x86_64/egg/sqlalchemy/ext/declarative/base.py", line 88, in _as_declarative file "build/bdist.linux-x86_64/egg/sqlalchemy/ext/declarative/base.py", line 103, in setup_mapping file "build/bdist.linux-x86_64/egg/sqlalchemy/ext/declarative/base.py", line 135, in __init__ file "build/bdist.linux-x86_64/egg/sqlalchemy/ext/declarative/base.py", line 138, in _early_mapping file "build/bdist.linux-x86_64/egg/sqlalchemy/ext/declarative/base.py", line 530, in map file "<string>", line 2, in mapper file "build/bdist.linux-x86_64/egg/sqlalchemy/orm/mapper.py", line 629, in __init__ file "build/bdist.linux-x86_64/egg/sqlalchemy/orm/mapper.py", line 1223, in _configure_pks sqlalchemy.exc.argumenterror: mapper mapper|records|records not assemble primary key columns mapped table 'records' am forced use primary key? or way call records class?
as error message indicates primary key mandatory. no reason not have 1 anyways, otherwise wont able express table relationships.
in response comment: if rely on using existing database scheme lacks primary keys can try zzzeek (the author of sqlalchemy) proposed in comment question: sqlalchemy declarative: table without primary keys?, use example
__mapper_args__ = {"primary_key":(data1, data2)}} if (data1, data2) identify table row. , there should way identify table table row, otherwise have severe design problem.
Comments
Post a Comment