MVC4:SQL登录失败。(MVC4: SQL Login failed. Error code 1. Only happening on one page)
我刚刚遇到了有史以来最奇怪(也是最令人沮丧)的错误之一。 我有一个刚刚被放到登台服务器上的网站(尚未上线)。 除了一个特定的页面,一切似乎都运行正常。 有一个方法被调用,它调用一个存储过程,但我得到一个
SqlException
,它说Login failed for [the-login-name]
错误状态为1.奇怪的是,从同一个方法调用完全相同的方法另一个页面,并使用完全相同的SQL连接详细信息,它很好..此外..这只发生在登台服务器上..不在我的开发机器上。 以前有人见过这样的事吗? 我无法弄清楚导致这种情况的2之间会有什么不同。我们都看到
it works on my dev machine
问题上工作..但是我从来没有看到调用相同方法的2个页面只会导致其中一个抛出异常...这只是简单的怪异。我正在使用MVC4和SQL Server 2012。
I have just come across what has to be one of the weirdest (and most frustrating) errors ever. I have a site which has just been put onto a staging server (not yet live). Everything seems to be working fine except one particular page. There is a method being called which invokes a stored procedure, but I am getting a
SqlException
which saysLogin failed for [the-login-name]
and an error state of 1. The curious thing is that the exact same method is being called from another page and is using the exact same sql connection details and it's fine.. moreover.. this is ONLY happening on the staging server.. not on my dev machine. Has anyone here ever seen anything like this before? I can't quite figure out what could be different between the 2 that would cause this.We've all seen the
it works on my dev machine
problem.. but I have never seen where 2 pages calling the same method causes only one of them to throw an exception.. that's just plain freaky.I am using MVC4 and SQL Server 2012.
原文:https://stackoverflow.com/questions/14040298
最满意答案
您还可以创建自定义模型字段类型 - 请参阅http://docs.djangoproject.com/en/dev/howto/custom-model-fields/#howto-custom-model-fields
在这种情况下,您可以从内置的IntegerField继承,并覆盖其验证逻辑。
我想到的越多,我就意识到对于许多Django应用程序来说,这有多有用。 也许一个IntegerRangeField类型可以作为Django开发人员考虑添加到trunk的补丁来提交。
这是为我工作:
from django.db import models class IntegerRangeField(models.IntegerField): def __init__(self, verbose_name=None, name=None, min_value=None, max_value=None, **kwargs): self.min_value, self.max_value = min_value, max_value models.IntegerField.__init__(self, verbose_name, name, **kwargs) def formfield(self, **kwargs): defaults = {'min_value': self.min_value, 'max_value':self.max_value} defaults.update(kwargs) return super(IntegerRangeField, self).formfield(**defaults)
然后在你的模型类中,你可以像这样使用它(字段是放在上面的代码的模块):
size = fields.IntegerRangeField(min_value=1, max_value=50)
或一个负和正的范围(如振荡器范围):
size = fields.IntegerRangeField(min_value=-100, max_value=100)
真的很酷的是如果可以用这样的范围操作符来调用它:
size = fields.IntegerRangeField(range(1, 50))
但是,这将需要更多的代码,因为您可以指定“skip”参数 - 范围(1,50,2) - 有趣的想法,但...
You could also create a custom model field type - see http://docs.djangoproject.com/en/dev/howto/custom-model-fields/#howto-custom-model-fields
In this case, you could 'inherit' from the built-in IntegerField and override its validation logic.
The more I think about this, I realize how useful this would be for many Django apps. Perhaps a IntegerRangeField type could be submitted as a patch for the Django devs to consider adding to trunk.
This is working for me:
from django.db import models class IntegerRangeField(models.IntegerField): def __init__(self, verbose_name=None, name=None, min_value=None, max_value=None, **kwargs): self.min_value, self.max_value = min_value, max_value models.IntegerField.__init__(self, verbose_name, name, **kwargs) def formfield(self, **kwargs): defaults = {'min_value': self.min_value, 'max_value':self.max_value} defaults.update(kwargs) return super(IntegerRangeField, self).formfield(**defaults)
Then in your model class, you would use it like this (field being the module where you put the above code):
size = fields.IntegerRangeField(min_value=1, max_value=50)
OR for a range of negative and positive (like an oscillator range):
size = fields.IntegerRangeField(min_value=-100, max_value=100)
What would be really cool is if it could be called with the range operator like this:
size = fields.IntegerRangeField(range(1, 50))
But, that would require a lot more code since since you can specify a 'skip' parameter - range(1, 50, 2) - Interesting idea though...
相关问答
更多-
TCP/IP模型是一个________。[2023-05-19]
a -
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
是的,有办法。 来自Django文档 : Field.default 字段的默认值。 这可以是值或可调用对象。 如果可调用,则每次创建新对象时都会调用它。 Yes, there is a way. From Django docs: Field.default The default value for the field. This can be a value or a callable object. If callable it will be called every time a new obj ...
-
如何限制Django模型中数字字段的最大值?(How to limit the maximum value of a numeric field in a Django model?)[2024-02-16]
您还可以创建自定义模型字段类型 - 请参阅http://docs.djangoproject.com/en/dev/howto/custom-model-fields/#howto-custom-model-fields 在这种情况下,您可以从内置的IntegerField继承,并覆盖其验证逻辑。 我想到的越多,我就意识到对于许多Django应用程序来说,这有多有用。 也许一个IntegerRangeField类型可以作为Django开发人员考虑添加到trunk的补丁来提交。 这是为我工作: from dj ... -
限制适用于ModelForm中的ModelForm ; 它们不是在模型或数据库级别执行的(尽管它们可能是可以的)。 The limits are applied to fields in a ModelForm; they are not enforced at the model or database level (although they conceivably could be).
-
django.db.utils.DataError:数字字段溢出 - django(django.db.utils.DataError: numeric field overflow - django)[2022-05-17]
错误消息表明您的表中存在的数字长度超过8位数(10-2)。 如果可能,您可以尝试修改有问题的行,或者增加max_digits以允许更大的数字。 The error message indicates that you have existing numbers longer than 8 digits (10-2) in your table. You might try modifying the offending rows if possible, or increase the max_digits ... -
用于PostgreSQL NUMERIC数据类型的Django模型字段(Django model field for PostgreSQL NUMERIC data type)[2022-04-14]
来自PostgreSQL文档: 小数和数字类型是等效的 。 这两种类型都是SQL标准的一部分。 所以你可以使用Django的DecimalField class MyModel(models.Model): my_field = models.DecimalField(max_digits=15, decimal_places=6) 一些文档: http://www.postgresql.org/docs/current/interactive/datatype-numeric.html#DATA ... -
用南 pip install South 首先,让你的django模型与你当前的数据库表完全匹配,然后运行: python manage.py schemamigration myapp --initial python manage.py migrate myapp --fake #Make changes to your django model (add new field) python manage.py schemamigration myapp --auto python manage. ...
-
这花了我很长时间才弄明白。 一个大问题是Django不支持JOIN语句的ON子句的多个条件。 由于单个SELECT语句依赖于两个单独的JOINs ,因此我们有一个问题是准确跟踪表名。 当你要求django注释一个表时,比如Max() ,你最终得到的条件是Max(T7.when) ... LEFT OUT JOIN table AS T7 ,其中T7不一致。 因此,在需要由Django自动生成的JOINs ,我需要一种方法来累积生成表达式Max(T7.when) 。 很多在线帖子都告诉你在查询集上使用.raw ...
-
查看这个Django Snippet的示例。 Check out this Django Snippet for an example.