我的数据库中有两个表:
我需要一些虚拟数据,所以我创建了以下SeedDummyOrders类:
<?php
class SeedDummyOrders {
public function run()
{
DB::table('order_details')->delete();
DB::table('order_header')->delete();
$inventory_ids = range(1, 10);
for ($i = 1; $i <= 5; $i++)
{
$order = new \OrderHeader;
$order->owner_id = 1;
$order->order_number = '100' . $i;
$order->delivery_type = 'Standard';
$order->order_status = 'Received';
$order->address_line1 = $i . ', Some Street';
$order->city = 'London';
$order->postcode = 'ABC D' . $i;
$order->country_code = 'GB';
$order->country_name = 'Great Britain';
$order->contact_name = 'Mr Person ' . $i;
$order->contact_email = 'contact' …
Run Code Online (Sandbox Code Playgroud) 我正在使用knex进行播种,并且有一个名为development的文件夹,其中包含所有种子文件。
我想要的是:如何播种单个文件。
我使用的命令是:knex seed:run --env = development但是此命令是播种所有文件,并且我在db上得到重复的行。
假设我昨天创建了种子文件,并且今天给它们添加了种子,我想添加另一个种子文件,我只想为该文件而不是昨天的文件添加种子。
Laravel的一个示例是:php artisan db:seed --class = ProductTableSeeder
谢谢
我有一个MongoDB的节点表达应用程序作为数据库.我想有一个seed.js文件,我可以运行它来填充数据库的初始数据.我在当地的机器上做这个没问题.我只是在我的seed.js文件中编写mongo命令并运行它:
$ mongo localhost:27017/myApp seed.js
Run Code Online (Sandbox Code Playgroud)
但是,当我将我的应用程序部署到Heroku(包括MongoLab)时,我不确定如何在那里播种数据.谷歌大部分时间都把我引向铁轨.
那么有没有一种简单的方法为Heroku MongoLab播种数据而无需编写脚本?
更新:如果我尝试使用heroku的MONGOLAB_URI运行它,我会收到错误.
$ mongo mongodb://heroku_xxxx:xxxxx.mongolab.com:xxx/heroku_xxxx seed.js
MongoDB shell version: 3.0.5
connecting to: mongodb://heroku_xxxxxxxx
2015-08-19T21:44:22.694+0100 E QUERY Error: More than one ':' detected. If this is an ipv6 address, it needs to be surrounded by '[' and ']'; heroku_xxxxxxxxxxxxx
at connect (src/mongo/shell/mongo.js:181:14)
at (connect):1:6 at src/mongo/shell/mongo.js:181
exception: connect failed
Run Code Online (Sandbox Code Playgroud) 我目前正在使用Sequelize.js播种数据,并将硬编码值用于关联ID。这不是理想的,因为我确实应该能够动态地执行此操作,对吗?例如,将用户和个人资料与“有一个”和“属于”关联相关联。我不一定要用硬编码为用户播种profileId
。创建配置文件后,我宁愿在配置文件种子中执行此操作。profileId
创建配置文件后,将动态地添加到用户。使用Sequelize.js可以做到这一点吗?还是在使用Sequelize播种时仅硬编码关联ID更为常见?
也许我要播种错误?使用Sequelize时,我是否应该拥有一对一的种子文件和迁移文件?在Rails中,通常只有1个种子文件,如果需要,您可以选择分成多个文件。
通常,仅在此处寻找指导和建议。这些是我的文件:
users.js
// User seeds
'use strict';
module.exports = {
up: function (queryInterface, Sequelize) {
/*
Add altering commands here.
Return a promise to correctly handle asynchronicity.
Example:
return queryInterface.bulkInsert('Person', [{
name: 'John Doe',
isBetaMember: false
}], {});
*/
var users = [];
for (let i = 0; i < 10; i++) {
users.push({
fname: "Foo",
lname: "Bar",
username: `foobar${i}`,
email: `foobar${i}@gmail.com`,
profileId: i + 1
});
}
return queryInterface.bulkInsert('Users', users);
},
down: function …
Run Code Online (Sandbox Code Playgroud) 我正在尝试从Laravel 5.6中的Seeder插入数据,但json类型的字段存在问题。我希望此字段(“停”)是一个数组(例如,十个不重复的整数)。
表播种器(RoutesTableSeeder.php)是这样的:
<?php
use \Illuminate\Support\Facades\DB;
use Illuminate\Database\Seeder;
use Faker\Factory as Faker;
use App\Models\Route;
class RoutesTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
//factory(Route::class, 20)->create();
$faker = Faker::create();
//$values= array();
/*for($i=0; $i < 10; $i++) {
$values []= $faker->unique()->randomDigit;
}
print_r(json_encode($values));*/
foreach (range(1, 20) as $index)
{
$values = array();
for($i=0; $i < 10; $i++) {
$values []= $faker->unique()->randomDigit;
}
//print_r($values);
DB::table('routes')->insert([
'user_id' => $faker->numberBetween($min = 1, $max = …
Run Code Online (Sandbox Code Playgroud) 我想经常清理并重新加载我的开发人员数据库(Ruby on rails).
当然,我可以通过网页手动添加数据,但我想知道是否有人有这种类型的测试策略.
(我已经有单元,功能和集成测试,fyi)
谢谢
我需要通过迁移和播种数据库来更新现有的 Laravel 应用程序。
我有一张如下表:
项目
我想编辑数据库看起来像:
项目
类型(新表)
随着TYPE_ID是一个非空的外键类型的表。
该类型的表将在接下来的操作中Laravel播种机播种。每次应用程序更新后都会调用此播种器,并截断/重新插入一些仅随应用程序更新而更改的“静态”数据。它的配置方式类似于Laravel : Migrations & Seeding for production data for the for the local/production差异
在本地数据库上从头开始时一切正常。但是在生产数据库上,已经有很多记录了。由于它是一个非空键,因此在推送到此数据库(实际上是该数据库的副本以供测试)时迁移失败
作为迁移解决方案,我想将第一个类型记录添加到每个现有项目,但我无法在迁移过程中设置外键,因为项目表在此阶段为空,并且在播种之前我不能将其留空,因为迁移没有通过。
我觉得有几点:
我该怎么办 ?
编辑:这是我使用@lukasgeiter 回答的工作迁移代码:
public function up()
{
DB::statement('SET FOREIGN_KEY_CHECKS = 0');
Schema::table('items', function(Blueprint $table)
{
$table->integer('type_id')->unsigned()->index()->after('name');
});
DB::update('update items set type_id = 1'); …
Run Code Online (Sandbox Code Playgroud) 我正在使用干预图像类进行laravel,并且正在复制,调整图像大小并将图像编码到sites目录.基本上模拟上传到虚假列表.
但是,在运行数据库种子时,我似乎遇到了内存问题.
错误信息:
local.ERROR: exception 'Symfony\Component\Debug\Exception\FatalErrorException'
with message 'Allowed memory size of 134217728 bytes exhausted (tried to allocate 5056 bytes)'
in C:\xampp\htdocs\equezone\vendor\intervention\image\src\Intervention\Image\Gd\Decoder.php:115
Run Code Online (Sandbox Code Playgroud)
每张图片不超过1265x625.仅当大于1300x700时,图像才会调整大小.因此,没有图像实际调整大小...
Gd\Decoder.php的第115行
$canvas = imagecreatetruecolor($width, $height);
Run Code Online (Sandbox Code Playgroud)
imagecreatetruecolor
似乎扩展了php的gd类.
这是我的代码的基础知识:
$image = Image::make(( ! is_string($file))? $file->getRealPath(): $file);
if ($image->width() > self::MAX_IMAGE_WIDTH || $image->height() > self::MAX_IMAGE_HEIGHT) {
self::resizeImage($image, self::MAX_IMAGE_WIDTH, self::MAX_IMAGE_HEIGHT);
}
/*
Some code here to retrieve the listing from the database,
create an image in the database
assign image to the listing
*/
$image->encode('jpg',100);
$image->save($img->getImageLocation(), 100);
Run Code Online (Sandbox Code Playgroud)
我找出了内存泄漏的来源.
在内存崩溃之前,种子将播种大约8-14个列表.上传大约60 …
我有以下代码seeds.rb
在我的简单Rails应用程序中创建一个记录.
Post.create(
title: "Unique Title!",
body: "this is the most amazingly unique post body ever!"
)
Run Code Online (Sandbox Code Playgroud)
运行该rake db:seed
命令时,它显然会使用此数据对db进行种子处理.如何在代码中添加检查或保护措施,使其仅进入一次,即作为唯一?如果我重新运行rake db:seed
,我不想再添加相同的条目.
我有几个属性(一个产品模型customer_type
,customer_name
,dept_type
等...),我想用一些数据种子吧.
我的db/seeds.rb
文件中有以下内容
Product.create(customer_type:'Retailer', customer_name: 'Walmart', dept_type: 'Grocery')
Run Code Online (Sandbox Code Playgroud)
我保存文件然后运行rake db:Seed
我得到没有错误消息,但当我加载我的应用程序时,没有数据存在?我在这做错了什么?
我也试过rake db:setup
,rake db:reset
每次都没有返回错误信息,但数据没有加载.
更新我修改了我的db种子文件,看起来像这样
Product.create!(customer_type:'Retailer', customer_name: 'Walmart', dept_type: 'Grocery')
Run Code Online (Sandbox Code Playgroud)
当我运行rake db:reset我得到错误"验证失败:客户类型不包含在列表中"
我的产品模型文件带有验证
class Product < ActiveRecord::Base
attr_accessible :customer_type
has_many :line_items
has_many :orders, through: :line_items
CUSTOMER_TYPES = ["Retailer", "Manufacturer"]
validates :customer_type, inclusion: CUSTOMER_TYPES
Run Code Online (Sandbox Code Playgroud)
我尝试使用客户类型值Retailer和制造商没有运气来播种数据库
seeding ×10
laravel ×3
php ×3
javascript ×2
laravel-4 ×2
mysql ×2
bookshelf.js ×1
database ×1
fixtures ×1
heroku ×1
knex.js ×1
memory-leaks ×1
mlab ×1
mongodb ×1
node.js ×1
ruby ×1
seed ×1
sequelize.js ×1
testing ×1