我无法从子类访问父类成员变量@Override
protected void configure(HttpSecurity http) throws Exception {
http.
authorizeRequests()
.antMatchers("/").permitAll()
.antMatchers("/index").permitAll()
.antMatchers("/login").permitAll()
.antMatchers("/confirmarRegistro").permitAll()
.antMatchers("/registration").permitAll()
.antMatchers("/admin/**").hasAuthority("ADMIN")
.antMatchers("/titular/**").hasAuthority("TITULAR")
.antMatchers("/sgc/**").hasAuthority("SGC")
.antMatchers("/usuario/**").hasAuthority("USUARIO").anyRequest()
.authenticated().and().csrf().disable().formLogin()
.loginPage("/index").failureUrl("/login?error=true")
.successHandler(successHandler)
.usernameParameter("email")
.passwordParameter("password")
.and().logout()
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
.logoutSuccessUrl("/").and().exceptionHandling()
.accessDeniedPage("/access-denied");
}
。
id
给我这个错误:
class BaseModel:
id = db.Column(db.Integer, primary_key=True)
class User(db.Model, BaseModel):
username = db.Column(db.String(35), nullable=False, unique=True)
followed = db.relationship(
'User',
secondary=followers,
primaryjoin=(followers.c.follower_id == BaseModel.id),
secondaryjoin=(followers.c.followed_id == BaseModel.id),
backref=db.backref('followers', lazy='dynamic'),
lazy='dynamic')
另一方面
sqlalchemy.exc.InvalidRequestError: One or more mappers failed to initialize - can't proceed with initialization of other mappers. Triggering mapper: 'Mapper|User|user'. Original exception was: Could not locate any simple equality expressions involving locally mapped foreign key columns for primary join condition 'followers.follower_id = "<name unknown>"' on relationship User.followed. Ensure that referencing columns are associated with a ForeignKey or ForeignKeyConstraint, or are annotated in the join condition with the foreign() annotation. To allow comparison operators other than '==', the relationship can be marked as viewonly=True.
给我这个错误:
class BaseModel:
id = db.Column(db.Integer, primary_key=True)
class User(db.Model, BaseModel):
username = db.Column(db.String(35), nullable=False, unique=True)
testing = BaseModel.id
答案 0 :(得分:1)
类定义的主体是它自己的范围,引入的名称将构成类的命名空间。模型定义的问题(在您的original question中)是在类id
的主体中尚未分配名称User
,因此在{{1}中加入了}定义是指内置函数id()
。您也不能使用此问题中所示的relationship
,因为Declarative元类将create a copy of it to User
,因此它不会引用同一列。
解决方案是使用惰性求值:将可调用或Python可求值的字符串作为连接传递:
BaseModel.id
之所以可行,是因为除非声明了configure_mappers()
明确配置,否则映射器的映射器间关系是在声明了映射的类之后配置的,并且是第一次使用。
请注意,您也不能在可评估字符串中使用普通的followed = db.relationship(
'User',
secondary=followers,
primaryjoin='followers.c.follower_id == User.id', # Note: the joins are
secondaryjoin='followers.c.followed_id == User.id', # passed as strings
backref=db.backref('followers', lazy='dynamic'),
lazy='dynamic')
,而必须使用id
类来引用它,因为稍后对其进行评估的范围与该类的范围完全不同正文including names from the Declarative class registry, and metadata。
最后一个错误是诸如
之类的代码的结果User
在类主体中将User.query.get(some_id)
分配给另一个名称(例如BaseModel.id
)会导致您的模型具有由2个整数列组成的复合主键,因此预期Query.get()
接收2个元组的整数,而不仅仅是一个整数。