SQL Server 2005 +实体设计器——外键约束错误?

发布于 2024-11-18 05:08:37 字数 1135 浏览 2 评论 0原文

使用VS2010中的实体设计器为SQL Server 2005构建数据库。当我尝试通过“从模型生成数据库”生成的SQL实际构建表时遇到问题;这些表都已正确创建,但它在外键约束上出现问题。

有问题的实体如下所示:

World:
  WORLD_ID [int32] (Primary Key)
  Name [String]

Zone:
  WORLD_ID [int32] (Primary key, FK on World.WORLD_ID)
  ZONE_ID [int32] (Primary key)
  Name [String]

Region:
  WORLD_ID [int32] (Primary key, FK on Zone.WORLD_ID)
  ZONE_ID [int32] (Primary key, FK on Zone.ZONE_ID)
  REGION_ID [int32] (Primary key)
  Name [String]

生成的 SQL 代码的这一部分是问题所在:

-- Creating foreign key on [ZONE_ID], [WORLD_ID] in table 'Regions'
ALTER TABLE [dbo].[Regions]
ADD CONSTRAINT [FK_ZoneRegion]
    FOREIGN KEY ([ZONE_ID], [WORLD_ID])
    REFERENCES [dbo].[Zones]
        ([ZONE_ID], [WORLD_ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

错误是这样的:

引用的表“dbo.Zone”中没有主键或候选键 与外键“FK_ZoneRegion”中的引用列列表匹配。

我必须假设这个问题与以下事实有关:在我的多列主键(WORLD_ID + ZONE_ID + REGION_ID)中,ZONE_ID和WORLD_ID都是外键,特别是WORLD_ID实际上也是包含 FK 源的表。

我这里的结构有问题吗?对我来说,只有一个来自 Zone -> 的关联不好吗?地区,其实我得有两个公会,一个来自世界->区域和一个来自区域 ->地区?

谢谢, -担

Using Entity Designer in VS2010 to build a database for SQL Server 2005. Having a problem when I try to actually build the tables via the SQL generated by "generate database from model"; the tables are all created correctly, but it croaks on the foreign key constraints.

The entities at issue are laid out here:

World:
  WORLD_ID [int32] (Primary Key)
  Name [String]

Zone:
  WORLD_ID [int32] (Primary key, FK on World.WORLD_ID)
  ZONE_ID [int32] (Primary key)
  Name [String]

Region:
  WORLD_ID [int32] (Primary key, FK on Zone.WORLD_ID)
  ZONE_ID [int32] (Primary key, FK on Zone.ZONE_ID)
  REGION_ID [int32] (Primary key)
  Name [String]

This part of the generated SQL code is the problem:

-- Creating foreign key on [ZONE_ID], [WORLD_ID] in table 'Regions'
ALTER TABLE [dbo].[Regions]
ADD CONSTRAINT [FK_ZoneRegion]
    FOREIGN KEY ([ZONE_ID], [WORLD_ID])
    REFERENCES [dbo].[Zones]
        ([ZONE_ID], [WORLD_ID])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

The error is this:

There are no primary or candidate keys in the referenced table 'dbo.Zone'
that match the referencing column list in the foreign key 'FK_ZoneRegion'.

I have to assume the issue has something to do with the fact that, in my multi-column primary key (WORLD_ID+ZONE_ID+REGION_ID), both ZONE_ID and WORLD_ID are foreign keys, and specifically that WORLD_ID is actually also a foreign key in the table with the source for the FK.

Is there something wrong with my structure here? Is it not OK for me to just have a single association going from Zone -> Region, and in fact I have to have two associations, one from World -> Region and one from Zone -> Region?

Thanks,
-Dan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月牙弯弯 2024-11-25 05:08:37

不管怎样,我最终只是从头开始重新构建实体图,并且它起作用了。不知道我的初始图表中是否有错误,或者这是否是实体设计器的故障。

For what it's worth, I ended up just re-building the entity diagram from scratch, and it worked. No idea whether I had an error in my initial diagram, or whether this was a glitch with Entity Designer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文