首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将主键更改为自动增量

将主键更改为自动增量
EN

Stack Overflow用户
提问于 2014-08-23 07:15:26
回答 1查看 1.5K关注 0票数 1

我在Doctrine 2/ Sf2中更新了我的数据库模式,并在数据库中添加了一些新表。一切都进行得很顺利,但是当我想创建应用程序的管理端时,我意识到我需要一些ID才能成为AUTO_INCREMENT。

然后,我想改变我的模式。从这里。

代码语言:javascript
复制
X\XBundle\Entity\Currency:
type: entity
table: currencies
repositoryClass: CurrencyRepository
id:
    id:
        type: integer
fields:
    name:
        type: string
        length: 255
    slug:
        type: string
        length: 64
manyToMany:
    brokers:
        targetEntity: X\XBundle\Entity\Broker
        mappedBy: currencies

对此:

代码语言:javascript
复制
X\XBundle\Entity\Currency:
type: entity
table: currencies
repositoryClass: CurrencyRepository
id:
    id:
        type: integer
        generator: { strategy: AUTO } # I changed on this row!
fields:
    name:
        type: string
        length: 255
    slug:
        type: string
        length: 64
manyToMany:
    brokers:
        targetEntity: X\XBundle\Entity\Broker
        mappedBy: currencies

然后,我尝试在CLI中运行php app/console doctrine:schema:update --force,并得到了以下消息:

代码语言:javascript
复制
 SQLSTATE[HY000]: General error: 1833 Cannot change column 'id': used in a foreign key constraint 'FK_706E39538248176' of table 'forexcbc.broker_currencies'

我现在什么都试过了。我手动删除外键并重新运行脚本,没有工作。我删除了这两张桌子之间的关系,没有用。有趣的是,桌子上什么都没有。

我的代理模式看起来像这样(我不知道这在任何形式上都有帮助)

代码语言:javascript
复制
X\XBundle\Entity\Broker:
type: entity
table: brokers
repositoryClass: BrokerRepository
id:
    id:
        type: integer
        generator: { strategy: AUTO }
fields:
    name:
        type: string
        length: 255
    slug:
        type: string
        length: 64
oneToMany:
    accountTypes:
        targetEntity: X\XBundle\Entity\AccountType
        mappedBy: broker
        cascade: ["persist"]
manyToMany:
    currencies:
        targetEntity: X\XBundle\Entity\Currency
        inversedBy: brokers
        joinTable:
            name: broker_currencies
            joinColumns:
                broker_id:
                    referencedColumnName: id
            inverseJoinColumns:
                currency_id:
                    referencedColumnName: id

我的问题是,在创建表之后,如何能够将ID字段设置为自动增量。再说一遍,桌子上什么都没有。

谢谢你提前提供帮助!

亚当

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-08-23 09:03:46

与将更新应用于数据库不同,您可以转储要应用的SQL:

代码语言:javascript
复制
app/console doctrine:schema:update --dump-sql --complete

打开mysql shell,选择DB并首先禁用外键检查:

代码语言:javascript
复制
SET FOREIGN_KEY_CHECKS = 0;

然后在shell中执行生成的SQL查询。

完成后,关闭shell,然后再次运行doctrine:schema:update命令,以查看一切是否成功。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25459439

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档