I'm using db:populate to preload some sample data into my rails project. For instance, I am using the following code to populate the db:
require 'faker'
namespace :db do
task :populate => :environment do
Rake::Task['db:reset'].invoke
100.times do |u|
User.create!(
:name => Faker::Name.name,
:email => Faker::Internet.email
)
end
puts "The count of user(s) is #{User.all.count}"
User.all.each do |u|
# Add some more info based for each user
end
end
end
However, what I get is an error when I run "rake db:populate". I get:
rake aborted!
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.each
I get this error on the call to puts where I print out the count of users.
If I reduce the 100.times down to about 10.times, the populate works correctly and the call to User.all.count responds with the correct value of 10.
The best that I can guess is that the call to "faker" gets overloaded and has not yet returned a value which causes the nil object. Maybe however, the populate is trying to run this as a single database transaction and is overloading some buffer.
Is there a way to "flush" the insert into the database so that each transaction is written to the database or pause while "faker" responds so I can create a larger data set to work with?
Thanks
Steve Woolley
swoolley#ardentsoft.com
I had the same problem
just running rake db:seed on a few console type expressions,
after doing a trace it says:
** Invoke environment (first_time)
** Execute environment
Now I don't have an environment 'first_time'
I looked at what I changed and it turns out I was calling by default a different environment in the initializer when attempting to connect to the db.
Take a look at your database.yml or application.rb files there might be a conflict
Cheers
Related
In my local/development environment, the MySQLi query is performing OK. However, when I upload it on my web host environment, I get this error:
Fatal error: Call to a member function bind_param() on a non-object in...
Here is the code:
global $mysqli;
$stmt = $mysqli->prepare("SELECT id, description FROM tbl_page_answer_category WHERE cur_own_id = ?");
$stmt->bind_param('i', $cur_id);
$stmt->execute();
$stmt->bind_result($uid, $desc);
To check my query, I tried to execute the query via control panel phpMyAdmin and the result is OK.
TL;DR
Always have mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); in your mysqli connection code and always check the PHP errors.
Always replace every PHP variable in the SQL query with a question mark, and execute the query using prepared statement. It will help to avoid syntax errors of all sorts.
Explanation
Sometimes your MySQLi code produces an error like mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given..., Call to a member function bind_param()... or similar. Or even without any error, but the query doesn't work all the same. It means that your query failed to execute.
Every time a query fails, MySQL has an error message that explains the reason. In the older PHP versions such errors weren't transferred to PHP, and all you'd get is a cryptic error message mentioned above. Hence it is very important to configure PHP and MySQLi to report MySQL errors to you. And once you get the error message, fixing it will be a piece of cake.
How to get the error message in MySQLi
First of all, always have this line before MySQLi connect in all your environments:
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
After that, all MySQL errors will be transferred into PHP exceptions. An uncaught exception, in turn, makes a PHP fatal error. Thus, in case of a MySQL error, you'll get a conventional PHP error. That will instantly make you aware of the error cause. And the stack trace will lead you to the exact spot where the error occurred.
How to get the error message from PHP
Here is a gist of my article on PHP error reporting:
Reporting errors on a development and live servers must be different. On the development server it is convenient to have errors shown on-screen, but on a live server error messages must be logged instead, so you could find them in the error log later.
Therefore, you must set corresponding configuration options to the following values:
On a development server
error_reporting should be set to E_ALL value;
log_errors should be set to 1 (it is convenient to have logs on a development PC too)
display_errors should be set to 1
On a production server
error_reporting should be set to E_ALL value;
log_errors should be set to 1
display_errors should be set to 0
After that, when MySQL query fails, you will get a PHP error that explains the reason. On a live server, in order to get the error message, you'll have to check the error log.
In case of AJAX call, on a dev server open DevTools (F12), then Network tab. Then initiate the request which result you want to see, and it will appear in the Network tab. Click on it and then the Response tab. There you will see the exact output. On a live server check the error log.
How to actually use it
Just remove any code that checks for the error manually, all those or die(), if ($result), try..catch and such. Simply write your database interaction code right away:
$stmt = $this->con->prepare("INSERT INTO table(name, quantity) VALUES (?,?)");
$stmt->bind_param("si", $name, $quantity);
$stmt->execute();
Again, without any conditions around. If an error occurs, it will be treated like any other error in your code. For example, on a development PC it will just appear on-screen, while on a live site it will be logged for the programmer, whereas for the user's convenience you could use an error handler (but that's a different story which is off topic for MySQLi, but you may read about it in the article linked above).
What to do with the error message you get
First of all you have to locate the problem query. The error message contains the file name and the line number of the exact spot where the error occurred. For the simple code that's enough, but if your code is using functions or classes you may need to follow the stack trace to locate the problem query.
After getting the error message, you have to read and comprehend it. It sounds too obvious if not condescending, but learners often overlook the fact that the error message is not just an alarm signal, but it actually contains a detailed explanation of the problem. And all you need is to read the error message and fix the issue.
Say, if it says that a particular table doesn't exist, you have to check spelling, typos, and letter case. Also you have to make sure that your PHP script connects to a correct database
Or, if it says there is an error in the SQL syntax, then you have to examine your SQL. And the problem spot is right before the query part cited in the error message.
If you don't understand the error message, try to google it. And when browsing the results, stick to answers that explain the error rather than bluntly give the solution. A solution may not work in your particular case, but the explanation will help you to understand the problem and make you able to fix the issue by yourself.
You have to also trust the error message. If it says that number of tokens doesn't match the number of bound variables then it is so. The same goes for the absent tables or columns. Given the choice, whether it's your own mistake or the error message is wrong, always stick to the former. Again it sounds condescending, but hundreds of questions on this very site prove this advise extremely useful.
A list of things you should never ever do in regard of error reporting
Never use an error suppression operator (#)! It makes a programmer unable read the error message and therefore unable to fix the error
Do not use die() or echo or any other function to print the error message on the screen unconditionally. PHP can report errors by itself and do it the right way depends on the environment - so just leave it for PHP.
Do not add a condition to test the query result manually (like if($result)). With error exceptions enabled such condition will just be useless.
Do not use the try..catch operator for echoing the error message. This operator should be used to perform some error handling, like a transaction rollback. But never use it just to report errors - as we learned above, PHP can already do it, the right way.
P.S.
Sometimes there is no error, but no results either. Then it means, there is no data in the database to match your criteria. In this case you have to admit this fact, even if you can swear the data and the criteria are all right. They are not. You have to check them again.
I've got an article that can help in this matter, How to debug database interactions. Although it is written for PDO, the principle is the same. Just follow those instructions step by step and either have your problem solved or have an answerable question for Stack Overflow.
I have a chunk of code that looks something like this:
Repo.transaction(fn ->
Repo.query!("set transaction isolation level serializable;")
# do some queries
end)
In my test suite, I continually run into the error:
(Postgrex.Error) ERROR 25001 (active_sql_transaction): SET TRANSACTION ISOLATION LEVEL must be called before any query
I'm wondering if I'm doing something fundamentally wrong, or if there's something about the test environment that I'm missing.
Thanks!
Not sure if you are still looking for the answer to this but I found a nice solution for this. For the case I have setup block like so:
setup tags do
:ok =
if tags[:isolation] do
Sandbox.checkout(Repo, isolation: tags[:isolation])
else
Sandbox.checkout(Repo)
end
unless tags[:async] do
Sandbox.mode(Repo, {:shared, self()})
end
:ok
end
then on the test that is in the path of the serializable transaction you have to tag it with "serializable" like so:
#tag isolation: "serializable"
test "my test" do
...
end
this will let you run your tests that come across serializable in the path and still use sandbox.
The problem is for testing purposes all of the tests are wrapped in a transaction so they can be rolled back so you don't pollute your database with tons of old test data. Which could result in failures that should have passed and passes that should have failed depending on how you've written your tests.
You can work around it but it will, again, pollute your test database and you'll have to clean it up yourself:
setup do
[Other set up stuff]
Ecto.Adapters.SQL.Sandbox.checkin(MyApp.Repo) #This closes any open transaction, effectively.
Ecto.Adapters.SQL.Sandbox.checkout(MyApp.Repo, [sandbox: false]) # This opens a new transaction without sandboxing.
end
This setup task goes in the test file with your failing tests if you don't have a setup. If you don't do the checkin call you'll (most likely) get an error about other queries running before the one setting the transaction level because you are inserting something before the test.
See here for someone essentially calling out the same issue.
I'm building a Sinatra app which will take a visitor's IP address and return the weather of that location. Following the Geocoder doc, it would seem that getting the IP is as simple as
city = request.location.city
Entering that into irb, however, just gives the NameError you see in the post title. Going what seems to me the logical route and attempting to create a new class upon which to use the request method results in another NameError - this time for the env hash taken by Rack::Request.new(env).
I can figure out how to build the thing on my own, but I'm stumped on this particular point, and assume that I am overlooking something not mentioned in the docs. What am I doing wrong that is causing request and env to remain undefined, and how do I go about correctly defining them?
It is as simple as that:
require "sinatra"
require "geocoder"
get "/" do
p request.location.city
"works"
end
Your problem is that you trying to run it in irb where you haven't the context of a request which creates and populates your request object. To play with it I would recommend just to run sinatra. Or use something like racksh, tux which gives you a something like irb but with all the context needed.
I am using in a script mysqli_query but i think there is something wrong with the syntaxes. The script did work before, but changing everything form mysql_query to mysqli_query, data is not put anymore correct in the database.
This was the original query:
mysql_query( "INSERT INTO download_manager (SET filename='".mysqli_real_escape_string($_GET['file'])."'),
ON DUPLICATE KEY UPDATE downloads=downloads+1");
I changed it with mysqli_query this way:
mysqli_query($link, "INSERT INTO download_manager (SET filename='".mysqli_real_escape_string($_GET['file'])."'),
ON DUPLICATE KEY UPDATE downloads=downloads+1");
Can someone tell me what i did wrong?
Update:
My connection looks like this:
$link = #mysqli_connect($db_host, $db_user, $db_pass, $db_database) or die ("Error " . mysqli_error($link));
mysql_set_charset('utf8');
Ensure that the handle stored in $link was generated from a call to mysqli_connect and not mysql_connect:
http://php.net/manual/en/function.mysqli-connect.php
If that fails, check for an error:
die(mysqli_error($link))
Troubleshooting
The mysql and mysqli PHP modules are distinct, and you can't mix functions between the modules.
Enable error reporting to ensure you are able to see useful error messages.
Ensure the database credentials and connection details are valid.
Use mysqli_error after mysqli_xxx function calls to detect issues within the mysqli module.
Use the # token sparingly to avoid hiding useful errors.
Cross-posted at MSDN.
I have a list of entity objects I'm trying to insert into a table in the local storage service. Using a data context class derived from the SampleClient TableStorageDataContext class, I create a new context object and add the entities with no issues. When I call context.SaveChanges(), an exception is ultimately thrown, but with sparingly little detail. I've run a profiler trace on SQL Server Express to see if the error originated there, and didn't find anything useful, which leads me to believe there's some issue in the local storage service, which I have no idea how to debug.
Here's the client code (F#):
let cxt0 = new WebRole.Models.TableDataContext()
entityList |> Seq.iter (fun n -> cxt0.AddObject("NutritionData", n))
let results = cxt0.SaveChanges()
I can set a breakpoint on the last of the above lines and stop execution and see that the cxt0 object contains all the entities to be added (>500K). After then continuing execution, the following exception is thrown:
System.Data.Services.Client.DataServiceRequestException:
"An error occurred while processing
this request."
at
System.Data.Services.Client.DataServiceContext.SaveAsyncResult.HandleBatchResponse()
at
System.Data.Services.Client.DataServiceContext.SaveAsyncResult.EndRequest()
at
System.Data.Services.Client.DataServiceContext.SaveChanges(SaveChangesOptions
options) at
System.Data.Services.Client.DataServiceContext.SaveChanges()
at
WorkerRole.SrDataProcessor.importSrData(FastFunc`2
pf, String blobName) in
C:\Users\Ben\Development\Projects\CloudProject\WorkerRole\SrDataProcessor.fs:line
76
The InnerException (pardon the brackets as I avoid the html tag scrubber):
System.Data.Services.Client.DataServiceClientException:
" [?xml version="1.0" encoding="utf-8"
standalone="yes"?] [error
xmlns="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"]
[code][/code] [message
xml:lang="en-US"]An error occurred
while processing this
request.[/message] [/error] "
at
System.Data.Services.Client.DataServiceContext.SaveAsyncResult.d__1d.MoveNext()
In the HandleBatchResponse method, it appears that this error may be reported as it enumerates through the responses. Any ideas what might be calling this? The only thing left I've thought to check but haven't is to ensure that none of my entities have string properties that go past 1000 characters.
Update: Now I have, and it doesn't look like there are any. The following snippet produced an empty sequence:
let longEntities =
nutData |> Seq.choose (fun nd -> if HasLongStringProperties(nd)
then Some(nd) else None)
Also, more generally, how does one debug issues like this? Is there any way to get some introspection into the local storage service?
Update 2: I've since discovered that the "exception of origin", so to speak, is actually a System.WebException reporting an "Internal Server Error (500)", with no further detail. I've done everything I know to do to ensure that the data I'm trying to insert is compatible with the schema and data types in the tables in the SQL Server Express database backing the table service, and still I don't know what the issue is. The TableDataService just won't accept the object I'm inserting. See the MSDN thread for more details. I've also opened a bug on connect.
I was trying to do an extremely simple demo and I had the same problem but I was finally able to figure it out. By running against the development storage with logging turned on:
See:
http://blogs.msdn.com/b/partlycloudy/archive/2009/12/16/development-storage-logging.aspx
After starting the DevelopmentStorage, running my app, and then stopping the development storage and looking in the log folder I see:
9/17/2010 1:30:47 PM [Error] Caught exception during performing table operation and rethrow: System.Exception: c:\Users\Scott\AppData\Local\Temp\jvcl5gjz.0.cs(14,23) : error CS0542: 'Number': member names cannot be the same as their enclosing type
at Microsoft.Cis.Services.Nephos.Table.Service.Protocols.Rest.TableManager.EndPerformOperation(IAsyncResult ar) in x:\rd\rd_store_stable\services\xstore\XFE\table\service\Protocols\REST\src\TableManager.cs:line 184
at Microsoft.Cis.Services.Nephos.Table.Service.Protocols.Rest.TableProtocolHead.d__3e.MoveNext() in x:\rd\rd_store_stable\services\xstore\XFE\table\service\Protocols\REST\src\TableProtocolHead.cs:line 732
It turns out that my demo was too simple, because I had created a table called Number with a single column called Number...