为什么我得到“TypeError:id()只接受一个参数(给定0)”?

时间:2015-10-25 15:13:43

标签: python sqlalchemy flask-sqlalchemy

我创建了一个<div class="post-main-area"> <a class="previous-slide-arrow" href="#">&lt;</a> <div class="post-slide">. <!--<div class="left-part">.</div> <div class="right-part">.</div>--> </div> <a class="next-slide-arrow" href="#">&gt;</a> </div>表。当我尝试填充它时,我得到time_zones说我需要包含TypeError值。

postgresql中的表:

id

来自\d+ time_zones Table "public.time_zones" Column | Type | Modifiers | Storage | Stats target | Description ------------+-----------------------------+---------------------------------------------------------+----------+--------------+------------- id | integer | not null default nextval('time_zones_id_seq'::regclass) | plain | | name | character varying(128) | not null | extended | | created_at | timestamp without time zone | | plain | | updated_at | timestamp without time zone | | plain | | Indexes: "time_zones_pkey" PRIMARY KEY, btree (id) "time_zones_name_key" UNIQUE CONSTRAINT, btree (name) Referenced by: TABLE "app_users" CONSTRAINT "app_users_time_zone_id_fkey" FOREIGN KEY (time_zone_id) REFERENCES time_zones(id) 的TimeZone类:

models.py

class TimeZone(db.Model): __table_name__ = 'time_zones' id = db.Column(db.Integer, db.Sequence('time_zones_id_seq'), primary_key=True, nullable=False) name = db.Column(db.String(128), nullable=False) created_at = db.Column(db.TIMESTAMP(timezone=False), default=_get_date) updated_at = db.Column(db.TIMESTAMP(timezone=False), onupdate=_get_date) 迁移文件:

Alembic

我尝试通过带有def upgrade(): op.create_table('time_zones', sa.Column('id', sa.Integer, primary_key=True, nullable=False), sa.Column('name', sa.String(128), nullable=False, unique=True), sa.Column('created_at', sa.TIMESTAMP(timezone=False), default=_get_date), sa.Column('updated_at', sa.TIMESTAMP(timezone=False), onupdate=_get_date)) 的烧瓶外壳向time_zones表添加名称:

TypeError

更新

如果我在>>> from app.main.models import TimeZone >>> from app import db >>> >>> tz = TimeZone(name='Central Time (US & Canada)') Traceback (most recent call last): File "<console>", line 1, in <module> File "<string>", line 2, in __init__ File "/usr/local/lib/python3.4/dist-packages/sqlalchemy/orm/instrumentation.py", line 347, in _new_state_if_none state = self._state_constructor(instance, self) File "/usr/local/lib/python3.4/dist-packages/sqlalchemy/util/langhelpers.py", line 747, in __get__ obj.__dict__[self.__name__] = result = self.fget(obj) File "/usr/local/lib/python3.4/dist-packages/sqlalchemy/orm/instrumentation.py", line 177, in _state_constructor self.dispatch.first_init(self, self.class_) File "/usr/local/lib/python3.4/dist-packages/sqlalchemy/event/attr.py", line 256, in __call__ fn(*args, **kw) File "/usr/local/lib/python3.4/dist-packages/sqlalchemy/orm/mapper.py", line 2860, in _event_on_first_init configure_mappers() File "/usr/local/lib/python3.4/dist-packages/sqlalchemy/orm/mapper.py", line 2756, in configure_mappers mapper._post_configure_properties() File "/usr/local/lib/python3.4/dist-packages/sqlalchemy/orm/mapper.py", line 1710, in _post_configure_properties prop.init() File "/usr/local/lib/python3.4/dist-packages/sqlalchemy/orm/interfaces.py", line 183, in init self.do_init() File "/usr/local/lib/python3.4/dist-packages/sqlalchemy/orm/relationships.py", line 1612, in do_init self._process_dependent_arguments() File "/usr/local/lib/python3.4/dist-packages/sqlalchemy/orm/relationships.py", line 1637, in _process_dependent_arguments setattr(self, attr, attr_value()) TypeError: id() takes exactly one argument (0 given) 中删除了我的AppUser类中的关系,我不会再出现此错误。所以我不能正确设置我的关系。

以下是app.auth.models models.py模块:

auth

1 个答案:

答案 0 :(得分:2)

疑似问题

我刚刚在自己的代码中解决了类似的问题,问题出在{“1}}上,定义了您在”UPDATE“中注释掉的关系。当您以这种方式声明排序时,SQLAlchemy会尝试使用AppUser的order_by=id属性进行排序,因为声明在AppUser类中。由于AppUser没有列或其他名为'id'的属性,因此python访问内置的id()方法,不能在没有参数的情况下使用(并且无论如何都不能用于排序关系)。

提议的解决方案

我敢打赌,将order_by参数更改为idorder_by=TimeZone.id可以清除错误;类似的更改修复了我的代码中的问题。