PostgreSQL引用错误(PostgreSQL referencing error)
我有一个房东表和一个清单表。 创建列表时,我收到此错误。
ERROR: there is no unique constraint matching given keys for referenced table "t_property_landlord" SQL state: 42830 create table if not exists t_listing( rental_listing_id BIGSERIAL primary key, landlord_id BIGSERIAL references t_property_landlord(landlord_id), available varchar(25), rent numeric, deposit numeric, description text, term varchar(25), pet boolean, feature JSON, post_ts date, view numeric, create_ts timestamp default now() );
这是房东表。
create table if not exists t_property_landlord( landlord_id BIGSERIAL, email varchar(100) references t_user(email), property_id BIGSERIAL references t_property(property_id), change_ts timestamp default now(), active boolean default true, primary key(email,property_id) );
我尝试创建create_ts,并引用landlord_id,使那些你独特但仍无法正常工作。 有人可以看看,让我知道我做错了什么? 我正在使用PostgreSQL 9.4
谢谢,
I have a landlord table and a listing table. when creating my listing table, I received this error.
ERROR: there is no unique constraint matching given keys for referenced table "t_property_landlord" SQL state: 42830 create table if not exists t_listing( rental_listing_id BIGSERIAL primary key, landlord_id BIGSERIAL references t_property_landlord(landlord_id), available varchar(25), rent numeric, deposit numeric, description text, term varchar(25), pet boolean, feature JSON, post_ts date, view numeric, create_ts timestamp default now() );
Here is the landlord table.
create table if not exists t_property_landlord( landlord_id BIGSERIAL, email varchar(100) references t_user(email), property_id BIGSERIAL references t_property(property_id), change_ts timestamp default now(), active boolean default true, primary key(email,property_id) );
I tried create create_ts, and reference the landlord_id, making those you unique but still not working. Can someone take a look and let me know what I'm doing wrong? I am using PostgreSQL 9.4
thanks,
原文:https://stackoverflow.com/questions/37867592
更新时间:2023-04-25 16:04
最满意答案
只需在您的测试用例setUp方法中使用超级用户登录即可
from django.test import TestCase from django.contrib.auth.models import User class TestThatNeedsLogin(TestCase): def setUp(self): User.objects.create_superuser( 'user1', 'user1@example.com', 'pswd', ) self.client.login(username="user1", password="pswd") def tearDown(self): self.client.logout() def test_something(self): response = self.client.get("/") self.assertEqual(200, response.status_code)
Just log in with a superuser in your test case setUp method
from django.test import TestCase from django.contrib.auth.models import User class TestThatNeedsLogin(TestCase): def setUp(self): User.objects.create_superuser( 'user1', 'user1@example.com', 'pswd', ) self.client.login(username="user1", password="pswd") def tearDown(self): self.client.logout() def test_something(self): response = self.client.get("/") self.assertEqual(200, response.status_code)
相关问答
更多-
超级用户不经许可处理,它们仅由is_superuser字段处理。 您可以使用@user_passes_test装饰器: from django.contrib.auth.decorators import user_passes_test @user_passes_test(lambda u: u.is_active and u.is_superuser) def my_view(request): ... Superusers are not handled by permission, th ...
-
is_staff不是一个权限,而不是permission_required你可以使用: @user_passes_test(lambda u: u.is_staff) 要么 from django.contrib.admin.views.decorators import staff_member_required @staff_member_required is_staff isn't a permission so instead of permission_required you could ...
-
CBV文档中列出了几个策略: 在你的urls.py路由中添加装饰器 ,例如login_required(ViewSpaceIndex.as_view(..)) 使用method_decorator装饰您的CBV dispatch方法,例如, from django.utils.decorators import method_decorator @method_decorator(login_required, name='dispatch') class ViewSpaceIndex(TemplateV ...
-
我可以在Django测试中避免permission_required装饰器吗?(Can I avoid permission_required decorator on Django testing?)[2022-09-21]
只需在您的测试用例setUp方法中使用超级用户登录即可 from django.test import TestCase from django.contrib.auth.models import User class TestThatNeedsLogin(TestCase): def setUp(self): User.objects.create_superuser( 'user1', 'user1@example.com', ... -
装饰链(Decorator chain)[2022-03-24]
您仍然需要调用您的装饰工厂: @special_permission() def function(): # ... 无论@ sign产生后的表达式用作装饰器; 装饰器被调用,传递函数来装饰。 换句话说,装饰器是语法糖: def function(): # ... special_permission()(function) 如果你离开()调用,函数会传递给你的包装器。 或者,让您的装饰器接受该函数,并将其直接传递给由permission_required_ajax()调用产生的装饰器: ... -
这就是我解决它的方式: from django.contrib.auth.decorators import user_passes_test from django.views.decorators.csrf import csrf_exempt def allow_all(self): return True @user_passes_test(allow_all) @csrf_exempt def callback_transloadit(request, pres_id): This ...
-
对django装饰(Right django decorator)[2022-05-17]
它更改了最里面的函数( _view ),因此它看起来与它包装的函数具有相同的名称,属性和文档。 这有助于在Python解释器中使用help() ,并使堆栈跟踪更清晰。 (注意:同样的事情基本上是由stdlib中包含的functools.wraps装饰器完成的。) It changes the innermost function (_view) so it appears to have the same name, attributes, and documentation as the function ... -
您应该使用user_passes_test装饰器来查看视图。 Django文档提供了一个很好的示例https://docs.djangoproject.com/en/1.8/topics/auth/default/#django.contrib.auth.decorators.user_passes_test 编辑:实际上您可以使用permission_required装饰器查看视图https://docs.djangoproject.com/en/1.8/topics/auth/default/#djan ...
-
尝试这个 return permission_decorator(func)(instance, request, *args, **kwargs) Try this return permission_decorator(func)(instance, request, *args, **kwargs)
-
django使用login_required装饰器测试视图尊重DRY(django testing views with login_required decorator respecting DRY)[2022-06-26]
如您所说,您可以将登录名移至setUp方法,以避免在测试类中的每个测试中出现重复。 如果您不喜欢在每个测试类中复制setUp方法,您可以创建自己的测试用例类或mixin。 class LoggedInTestCase(TestCase): def setUp(self): user = User.objects.create_user(username='username', password='password') self.client.login(userna ...