- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在尝试使用 UUID 作为 Laravel 4 的主键。
没有找到关于这个主题的太多信息,我决定在 MySQL 中使用触发器在插入时将 id 列设置为 UUID() 的值。
根据我的阅读,我需要在
的模型中设置一个 varpublic $incrementing = false;
在我的迁移文件中,每个表都有这样的内容:
//set default id to UUID, requires thread_stack in my.cnf to be 196k
function makeTrigger($tableName)
{
DB::unprepared('CREATE TRIGGER '.$tableName.'_trigger_id BEFORE INSERT ON '.$tableName.' FOR EACH ROW SET NEW.id = UUID()');
}
$tableName = 'sites';
Schema::create($tableName, function($table)
{
$table->string('id', 36)->primary();
$table->string('name', 50);
$table->string('development_url', 50);
$table->string('production_url', 50);
$table->boolean('launched')->default(0);
$table->string('test');
$table->timestamps();
$table->softDeletes();
});
makeTrigger($tableName);
虽然我可以插入像这样具有 UUID 的记录,但如果在模型中设置了 $incrementing = false,我将无法返回 ID。
如果我删除那个 var 并且我使用的是 UUID,则返回的 id 是 0。如果我在迁移文件中使用 increments('id'),我会得到返回的真实 id。
我正在为规范中的 ids 构建一个 UUID 的应用程序,所以我想弄清楚这在 Laravel 4 中是否可行。
如果我无法使用
取回该 ID$user = User::create($userdata);
return $user->id;
那么id将如何用于关系呢? (同样的问题使用 $user->save();)
据我所知,Eloquent 期待返回一个 auto_increment,但似乎应该有办法取回任何 id。
为什么这行不通?
任何对此领域的见解都将不胜感激,因为我似乎找不到关于该主题的任何真实文档。
最佳答案
我通过在模型上使用 creating
事件来解决这个问题,以便在保存模型时添加新的 UUID。您还可以在 packagist 上找到我的解决方案.
class BaseModel extends Eloquent {
/**
* Indicates if the IDs are auto-incrementing.
*
* @var bool
*/
public $incrementing = false;
/**
* The "booting" method of the model.
*
* @return void
*/
protected static function boot()
{
parent::boot();
static::creating(function($model)
{
$model->{$model->getKeyName()} = (string)$model->generateNewId();
});
}
/**
* Get a new version 4 (random) UUID.
*
* @return \Rhumsaa\Uuid\Uuid
*/
public function generateNewId()
{
return \Rhumsaa\Uuid\Uuid::uuid4();
}
}
关于mysql - Laravel LastInsertId 与 UUID PK,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17280616/
甲骨文: create table t7(c1 number primary key,c2 number); insert into t7 values (1,3); insert into t
谁能告诉我为什么点击模板链接生成的url是/.pk。我想了解 url 是如何工作的。这里是 Django 新手。 Traceback (most recent call last): File "
我有以下 MySQL 触发器。它因 tbl_users 的更新而被触发。如果当天已经存在记录,它将用一些信息更新 tbl_user_stats;如果当天没有记录,它将在 tbl_user_stats
已知:tX_1.name 永远不能为 NULL,长度可变,最多 45 个字符,并且必须始终是唯一的。 哪些因素会影响是否应使用代理 PK 加唯一列(架构 1)或 PK 自然键是否使用第一个表的唯一列作
我有一个包含复合 PK(code_id、user_id)的表,它已经有 100k 条记录。从复合 PK 移动到单个 PK 是否安全,如下例所示? ALTER TABLE orders DROP C
为什么这个模型没有创建 pk 并提示相关模型的 pk 的完整性? 创建 UserProfile 的新实例时,它不会创建主键。 我正在遵循一对一的说明 in this tutorial (这就是所有 @
注意:我是 JPA 开发的新手,正在快速学习试用,所以请原谅我可能遗漏的任何明显内容。 以下是相关的 JPA 实体。地址未列出,但它是一个非常简单的实体,具有一个名为 id 的 @Id。 ShipTo
我们的系统中有两个实体,如下所示: Invoice:[InvoiceID, OrgID] 是表示为 bean 的主键,并使用 @IdClass 注释与 Invoice 相关联 WorkflowStat
场景:我遇到了一些在事务中将 JPA 与 JDBC 混合的代码。 JDBC 正在对基本上是空白行的表执行 INSERT,将主键设置为 (SELECT MAX(PK) + 1) 并将 middleNam
我有一个包含 3 个表的数据库:类别、项目、关系。 categories 包含类别。商品存储在items中,relation是一个绑定(bind)表,它存储了产品id和类别id或类别。 对于给定的项目
问题 当我尝试导入 CSV 文件时,我收到 key “PRIMARY”的重复条目“x-x-xx-x-x-x” - PK 错误。 PK 本身是两个连接的 varchar。据我所知,连接永远不会重复。 我
我正在尝试使用连接从两个大表中进行选择: EXPLAIN SELECT SQL_NO_CACHE e.* FROM `table_A` e JOIN (SELECT id FROM
我有这个 post_list.html 文件:(忽略第二个 'blog:post_detail' url) {% for post in post_list %} {{ post.title }}
我需要使用 JPA 处理现有数据库表。这些表使用复合主键。外键属性与表的主键重叠。 简化示例,每个“订单”都有许多“OrderItems” Table Order
据我所知,每当我在 JPA/Hibernate 实体内的 Long 字段上使用 @Id 和 @GeneratedValue 时,我实际上是在使用代理键,我认为这是定义主键的一种非常好的方式,考虑到我在
所以我有这段代码: request = self.factory.get(reverse('portal-edit-automation', args=(self.rule.id,))) respon
当在MySQL中使用InnoDB存储引擎时,如果在创建表时不指定PRIMARY KEY,则使用隐藏唯一索引作为聚集索引。由于数据字典上的互斥锁,我了解到这些隐藏索引可能会导致争用。 我的问题是 - 如
我正在尝试删除 table1 中与 table2 中具有匹配 PK 的所有行。尽管我的 WHERE 子句使用了键,但我收到错误 1175。我熟悉切换安全模式,但这不应该成为问题,因为我的 WHERE
我正在尝试更新具有外键字段的 View 的记录,因此出现错误,因为我尝试更新没有外键字段的另一个模型并且效果很好。 还有其他类似的问题,但就我而言,我通过了 pk。 urls.py urlpatte
Beeferman 的 PK 和 WindowDIFF 的 Python NLTK 实现从两者的 python segeval 实现中得到完全不同的结果。 使用相同的参数。 hyp: 01001000
我是一名优秀的程序员,十分优秀!